为什么第二个for循环只迭代一次?

时间:2013-09-18 15:37:28

标签: python csv

import csv
with open("t1.csv", "rb") as f:
    reader = csv.reader(f)
    with open("t2.csv", "rb") as e:
         reader2 = csv.reader(e)
         with open("alnew.csv", "wb") as cr:
             writer = csv.writer(cr)
             for row in reader:
                 print row + ["XXXXX"]
                 for row2 in reader2:
                      print row2 + ["OOOOO"]
                      if row[0] == row2[3]:
                          new = row + row2
                          writer.writerow(new)

第二个for循环不应该每次遍历第一个循环时迭代吗?它似乎只是第一次运行循环时运行。

3 个答案:

答案 0 :(得分:3)

问题是您只打开第二个文件的文件句柄一次。第二次通过循环它已经筋疲力尽了。像其他生成器一样的文件句柄只能迭代一次而不重新创建它们。您可以每次重新打开该文件。但是在这种情况下我更喜欢itertools.product

import csv, itertools
with open("t1.csv", "rb") as f, with open("t2.csv", "rb") as e, with open("alnew.csv", "wb") as cr:
    reader = csv.reader(f)
    reader2 = csv.reader(e)
    writer = csv.writer(cr)
    for row, row2 in itertools.product(reader, reader2)
        if row[0] == row2[3]:
            writer.writerow(row + row2)

答案 1 :(得分:2)

您需要“重置”阅读器。正如它所写,reader2将到达文件的末尾,但不知道它应该回到顶部。

要重新读取文件,您可以重新打开该文件并重新开始。

import csv
with open("t1.csv", "rb") as f:
    reader = csv.reader(f)
    with open("alnew.csv", "wb") as cr:
        writer = csv.writer(cr)
        for row in reader:
            print row + ["XXXXX"]
            with open("t2.csv", "rb") as e:
                reader2 = csv.reader(e)
                for row2 in reader2:
                     print row2 + ["OOOOO"]
                     if row[0] == row2[3]:
                         new = row + row2
                         writer.writerow(new)

答案 2 :(得分:0)

一旦读完t2.csv一次,读者(和基础文件)就会保留在文件的“末尾”。它们不会因为你试图再次迭代它们而重置为开头。

对于常规文件,您可以使用seek方法来处理此问题,但是(除非我遗漏了某些内容)csv.reader没有seek方法,并且可能如果您在基础文件上调用seek,我们将不满意您。因此,最简单的解决方法是将整个t2.csv读入数组:更改

     reader2 = csv.reader(e)

     reader2 = [row for row in csv.reader(e)]

如果t2.csv太大而无法实现,我认为你必须两个调用seek(0)文件,并销毁并重新创建reader2 ,每次都通过外循环。