再次,如何让嵌套循环在python中工作

alv*_*vas 0 python loops

有人可以用这个嵌套循环帮助我吗?它有与Loops不工作相同的问题- 字符串(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]
Run Code Online (Sandbox Code Playgroud)

此代码的目的是检查与当前文本文件(即阅读器)一致的引用文本(即reader2)中的行.然后打印reference.txt中的序列号

reference.txt看起来像这样(序列号和句子之间的空格是a tab):

S00001LP    this is a nested problem
S00002LP    that cannot be solved
S00003LP    and it's pissing me off
S00004LP    badly
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

所需的输出将如下所示:

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
Run Code Online (Sandbox Code Playgroud)

Ned*_*der 6

您只能从流中读取一次.您的内部循环过快地消耗第二个文件,并且外部循环的其他迭代没有机会再次读取第二个文件.

尝试改变这个:

reader = csv.reader(open("reference.txt"), delimiter = "\t")
reader2 = csv.reader(open("current.txt"), delimiter = "\t")
Run Code Online (Sandbox Code Playgroud)

对此:

reader = list(csv.reader(open("reference.txt"), delimiter = "\t"))
reader2 = list(csv.reader(open("current.txt"), delimiter = "\t"))
Run Code Online (Sandbox Code Playgroud)

list()将完整地阅读文件,创建从它在内存中的列表,然后你就可以重复多次作为你喜欢.

更好的解决方案是将参考数据存储在字典中,这样您就不必为数据中的每一行循环它.