Python循环检查列

Python循环检查列,python,loops,Python,Loops,我试图检查以制表符分隔的文件的第3列是否包含某个单词。 如果没有,则应继续阅读。如果它确实包含单词,则应选中第4列。根据第4列中是否有内容,输出应为找到的内容或未找到的内容 我没有陷入第二部分,即检查第4列。我的输出给了我“找到的东西”,而实际上那里没有内容 for line in f: if line.strip()split("\t")[2] == "word": print ("word") if line.strip().split("\t")[3

我试图检查以制表符分隔的文件的第3列是否包含某个单词。 如果没有,则应继续阅读。如果它确实包含单词,则应选中第4列。根据第4列中是否有内容,输出应为找到的内容或未找到的内容

我没有陷入第二部分,即检查第4列。我的输出给了我“找到的东西”,而实际上那里没有内容

for line in f:
    if line.strip()split("\t")[2] == "word":
        print ("word")
        if line.strip().split("\t")[3] is not None:
            print ("something found")
        else:
            print("nothing found")
该文件如下所示:

Header1,Header2,Header3,Header4
item1.1,item1.2,item1.3,item1.4
item2.1,item2.2,item2.3,item2.4
参考资料#1参考资料#2 notword内容…(更多栏目)
参考资料#1参考资料#2字内容…
参考文献#1参考文献#2单词无内容

首先(我喝醉了,在酒吧里回答斯塔科弗流的问题)。读一行。如果第[2]行是“word”,那么如果第[3]行存在,则为chekk。如果有。。做你想做的。如果没有。。做点别的事lile

f = open("sample.txt", "r")
for line in f:
    line_array = line.split("\t"):
    if line_array[2] == 'word'
        try:
            if line[3] == "what you want"
            do_function()
        exception Exception e:
            print "no fourth columm"
对不起,他语法不好。。首先,我只是想帮忙(在酒吧里回答斯塔科弗劳的问题时我喝醉了)。读一行。如果第[2]行是“word”,那么如果第[3]行存在,则为chekk。如果有。。做你想做的。如果没有。。做点别的事lile

f = open("sample.txt", "r")
for line in f:
    line_array = line.split("\t"):
    if line_array[2] == 'word'
        try:
            if line[3] == "what you want"
            do_function()
        exception Exception e:
            print "no fourth columm"

对不起,他语法不好。。只是想帮助你,你有双重嵌套的循环

这将循环遍历每个单元格并检查内容是否为word

f.readlines()中的行的
:
对于第.split(“\t”)行中的项目:
如果项目==“单词”:
做某事
其他:
做点别的事
对于标题,您可以这样做

您还可以使用python模块为您解析CSV,这样您就不必担心诸如引号之类的事情

导入csv
以open('sample.txt','rb')作为f:
reader=csv.reader(f,分隔符='\t')
对于读取器中的行:
如果len(row)>2且row[2]=“您的字符串”:
foo()

对于与大型数据表交互,该模块也非常有用。

您有双重嵌套循环

这将循环遍历每个单元格并检查内容是否为word

f.readlines()中的行的
:
对于第.split(“\t”)行中的项目:
如果项目==“单词”:
做某事
其他:
做点别的事
对于标题,您可以这样做

您还可以使用python模块为您解析CSV,这样您就不必担心诸如引号之类的事情

导入csv
以open('sample.txt','rb')作为f:
reader=csv.reader(f,分隔符='\t')
对于读取器中的行:
如果len(row)>2且row[2]=“您的字符串”:
foo()
对于与大型数据表的交互,该模块也非常有用。

(我使用逗号而不是制表符,以便在答案中更容易看到…)

因此,您有一个如下所示的文件:

Header1,Header2,Header3,Header4
item1.1,item1.2,item1.3,item1.4
item2.1,item2.2,item2.3,item2.4
是这样吗

您试图首先检查
itemX.3
位置中的值,如果这与您要查找的不匹配,您想检查
itemX.4
的位置吗

with open('sample.txt', 'r') as f:
  for line in f:  # Loop through each line.
    items = line.split(',')  # Split the line into pieces.

    if len(items) > 2 and items[2] == 'the value you\'re looking for':
      print 'Found it!'
    elif len(items) > 3 and items[3] == 'the value you\'re looking for':
      print 'Found it!'
    else:
      continue  # Go to the next line.
有道理吗?

(我使用逗号而不是制表符,以便在答案中更容易看到…)

因此,您有一个如下所示的文件:

Header1,Header2,Header3,Header4
item1.1,item1.2,item1.3,item1.4
item2.1,item2.2,item2.3,item2.4
是这样吗

您试图首先检查
itemX.3
位置中的值,如果这与您要查找的不匹配,您想检查
itemX.4
的位置吗

with open('sample.txt', 'r') as f:
  for line in f:  # Loop through each line.
    items = line.split(',')  # Split the line into pieces.

    if len(items) > 2 and items[2] == 'the value you\'re looking for':
      print 'Found it!'
    elif len(items) > 3 and items[3] == 'the value you\'re looking for':
      print 'Found it!'
    else:
      continue  # Go to the next line.

有道理吗?

这应该可以满足您的需求:

for line in f:
    if line.strip().split("\t")[2] == "word":
        if line.strip().split("\t")[3] is not None:
            print "something found!"
        else:
            print "nothing found!"

另外,我注意到其他解决方案并没有特别关注第2列和第3列,这里就是这一列。我想我应该指出这一点。

这应该可以满足您的需求:

for line in f:
    if line.strip().split("\t")[2] == "word":
        if line.strip().split("\t")[3] is not None:
            print "something found!"
        else:
            print "nothing found!"

另外,我注意到其他解决方案并没有特别关注第2列和第3列,这里就是这一列。我想我应该指出这一点。

根据我的理解,你可以无条件地测试第四项,即使第三项不是“单词”,你也应该将第二项
放在第一项之内

  • 在文件的行上循环
  • 将每一行剥离并拆分为列表中的元素
  • 如果有第三项且该项等于字符串“word”
    • 无条件打印字符串“word”
    • 如果有第四项,且该项不是空字符串,则打印字符串“something found”
    • 否则,打印字符串“nothing found”
  • 编辑

    根据OP的发现,我意识到
    split
    的默认行为是在空白序列上分割,因为文档字符串中清楚地记录了它,尽管使用了不同的措辞

    下面的示例也显示了这一点

    >>> a = 'a\tb\tc\t\te'
    >>> a.split()
    ['a', 'b', 'c', 'e']
    >>> a.split('\t')
    ['a', 'b', 'c', '', 'e']
    >>> 
    
    总而言之,OP应该使用以下代码

     for line in open('data.tsv'):
        items = line.strip().split('\t')
        # -------------------------^^^^--------
        if len(items)>2 and items[2] == "word":
             print "word"
             if len(items)>3 and items[3] != "":
                 print "something found"
             else:
                 print "nothing found"
    

    根据我的理解,您无条件地测试第四项,即使第三项不是“单词”,您也应该将第二项
    if
    放在第一项内

  • 在文件的行上循环
  • 将每一行剥离并拆分为列表中的元素
  • 如果有第三项且该项等于字符串“word”
    • 无条件打印字符串“word”
    • 如果有第四项,且该项不是空字符串,则打印字符串“something found”
    • 否则,打印字符串“nothing found”
  • 编辑

    根据OP的发现,我意识到
    split
    的默认行为是在空白序列上分割,因为文档字符串中清楚地记录了它,尽管使用了不同的措辞

    下面的示例也显示了这一点

    >>> a = 'a\tb\tc\t\te'
    >>> a.split()
    ['a', 'b', 'c', 'e']
    >>> a.split('\t')
    ['a', 'b', 'c', '', 'e']
    >>> 
    
    总而言之,OP应该使用以下代码

     for line in open('data.tsv'):
        items = line.strip().split('\t')
        # -------------------------^^^^--------
        if len(items)>2 and items[2] == "word":
             print "word"
             if len(items)>3 and items[3] != "":
                 print "something found"
             else:
                 print "nothing found"
    

    你能展示你的文件的一部分吗?你认为对f:
    中的每一行做
    ,对f:
    中的每一行做
    ,你能完成什么?@KasraAD,编辑有帮助吗?@jwodder,我原以为可以阅读每一行,但我把它拿出来,因为没有必要。谢谢你能出示你档案的一部分吗?你认为你会做什么