1
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 循环不应该在每次通过第一个循环时迭代吗?它似乎只在第一次通过循环时运行。

4

3 回答 3

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)
于 2013-09-18T16:01:52.733 回答
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)
于 2013-09-18T15:50:54.420 回答
0

一旦您通读t2.csv一遍,阅读器(和基础文件)将保持在文件的“末尾”。它们不会仅仅因为您尝试再次迭代它们而重置为开始。

对于常规文件,您可以使用该seek方法来处理此问题,但是(除非我遗漏了什么)csv.reader没有方法,并且如果您调用基础文件seek可能不会对您感到满意。seek所以最简单的解决方法是将整个 of 读t2.csv入一个数组:更改

     reader2 = csv.reader(e)

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

如果t2.csv太大而无法实用,我认为您必须每次通过外循环调用seek(0)文件,并 destroy 和 recreate 。reader2

于 2013-09-18T15:54:46.090 回答