有人可以帮助我使用这个嵌套循环吗?它与Loops not working - Strings (Python)具有相同的问题,但现在它在csv类中没有csv.readline()函数。
import csv
import sys, re
import codecs
reload(sys)
sys.setdefaultencoding('utf-8')
reader = csv.reader(open("reference.txt"), delimiter = "\t")
reader2 = csv.reader(open("current.txt"), delimiter = "\t")
for line in reader:
for line2 in reader2:
if line[0] == line2[1]:
print line2[0] + '\t' + line[0]
print line[1]
else:
print line[0]
print line[1]
此代码的目的是检查与当前文本文件(即阅读器)一致的参考文本(即reader2)中的行。然后打印reference.txt中的序列号
reference.txt看起来像这样(序列号和句子之间的空格是tab
):
S00001LP this is a nested problem
S00002LP that cannot be solved
S00003LP and it's pissing me off
S00004LP badly
current.txt看起来像这样(第1句和第2句之间的空格是a):
this is a nested problem wakaraa pii ney bay tam
and i really can't solve it shuu ipp faa luiip
so i come to seek help from stackoverflow lakjsd sdiiije
seriously it is crazy because such foo bar bar foo
problems don't happen in other languages whaloemver ahjd
and it's pissing me off gaga ooo mama
badly wahahahah
所需的输出将如下所示:
S00001LP this is a nested problem wakaraa pii ney bay tam
and i really can't solve it shuu ipp faa luiip
so i come to seek help from stackoverflow lakjsd sdiiije
seriously it is crazy because such foo bar bar foo
problems don't happen in other languages whaloemver ahjd
S00003LP and it's pissing me off gaga ooo mama
S00004LP badly wahahahah
答案 0 :(得分:6)
您只能从流中读取一次。你的内部循环过快地消耗了第二个文件,你的外部循环的其他迭代没有机会再次读取第二个文件。
尝试更改此内容:
reader = csv.reader(open("reference.txt"), delimiter = "\t")
reader2 = csv.reader(open("current.txt"), delimiter = "\t")
到此:
reader = list(csv.reader(open("reference.txt"), delimiter = "\t"))
reader2 = list(csv.reader(open("current.txt"), delimiter = "\t"))
list()
将完整地读取文件,从中创建一个内存列表,然后您可以根据需要进行多次迭代。
更好的解决方案是将您的参考数据存储在字典中,这样您就不必为数据中的每一行循环它。
答案 1 :(得分:4)
一种方法是创建一个将键映射到序列号的字典:
serials = dict(map(reversed, reader))
for line in reader2:
serial = serials.get(line[0])
if serial is not None:
print serial
这比嵌套循环快得多。
第一行创建字典映射键到序列号。由于字典构造函数在文件实际包含(值,键)对时期望(键,值)对的迭代,我们必须交换每个记录中的两个条目 - 这是map(reversed, ...)
所做的。