简体   繁体   English

迭代字典并打印tsv文件中的匹配行

[英]Iterating through dictionary and printing matching rows from tsv file

I have a tsv file like this with headers. 我有一个这样的tsv文件与标题。

Header1 header2 header3 header4 header5
Aa      bb      dd      cc      aa      
Bb      bb      aa      cc      bb    
Cc      bb      cc      dd      aa    
Aa      bb      ee      cc      dd    
Aa      vv      ff      gg      ii

I have a dictionary like {'0': 'aa', '1':'bb','3':'cc'} 我有一个字典,如{'0': 'aa', '1':'bb','3':'cc'}

I am supposed to parse through this file, and return rows from the file where every column of index 0 is aa, index 1 is bb and index 3 is cc. 我应该解析这个文件,并返回文件中的行,其中索引0的每列是aa,索引1是bb,索引3是cc。 In other words, I need to get all the rows where first column is aa, second column is bb and 4th column is cc. 换句话说,我需要得到第一列为aa,第二列为bb,第四列为cc的所有行。 So I should be able to print the first 1st and the 4th row from the tsv file, which are 所以我应该能够从tsv文件中打印第一行和第四行,这些行是

Aa  bb  dd  cc  aa
Aa  bb  ee  cc  dd

My code snippet does not give the intersection of all these conditions but gives the all the rows where each one of the condition satisfies. 我的代码片段没有给出所有这些条件的交集,而是给出了每个条件满足的所有行。 Please help me correct my script. 请帮我纠正我的脚本。 The dictionary specified above is named as index dict. 上面指定的字典被命名为索引字典。

data=csv.reader(open(tsvfile,'rb'),delimiter = "\t")
            fields =data.next()
            print "-------------------------Rows Filtered-------------------------"
            for key,value in indexdict.items():

              for row in data:

                            if row[key]== value:
                                    linecount=linecount+1
                                    print row`                  

The all builtin function is what you need: all内置功能都是您所需要的:

for row in data:
    if all(row[key] == value for key, value in indexdict.items()):
        print row

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM