【问题标题】:python compare two csv files with csv module: not iterating through every rowpython将两个csv文件与csv模块进行比较:不遍历每一行
【发布时间】:2012-09-07 04:35:49
【问题描述】:

我正在尝试比较两个 csv 文件,然后在每个 csv 文件的一行中打印 product_id 字段。这是有问题的代码。需要注意的是,在两个csv文件中,字段的顺序是不一样的。

import csv
import sys

f1 = sys.argv[1]
f2 = sys.argv[2]
num_matches = 0

with open(f1, 'rb') as f:
    csv_readerf = csv.reader(f)
    csv_readerf.next()
    with open(f2, 'rb') as n:
        csv_readern = csv.reader(n)
        csv_readern.next()
        for row in csv_readerf:
            a_name  = row[0].replace(" ", "").lower()   #not used, can be ommitted
            a_id    = row[1]
            a_post  = row[2]
            a_rev   = row[3]
            a_loc   = row[4]                            #not used, can be ommitted
            a_desc  = row[5].replace(" ", "").lower()   #remove all whitespaces for uniformity
            a_ovr   = row[6]
            a_cmf   = row[7]
            a_sty   = row[8]
            a_siz   = row[9]
            a_arc   = row[10]
            a_wid   = row[11]
            a_url   = row[12]
            for rowP in csv_readern:
                p_name  = rowP[10].replace(" ", "").lower()
                p_id    = rowP[6]

                temp    = rowP[11].split(" ")[0:3]      #disregard time stamp
                p_post  = (" ").join(temp)

                p_rev   = rowP[7]
                if p_rev is "":
                    p_rev = "Anonymous"
                p_desc  = rowP[1].replace(" ", "").replace("\n", "").replace("\r\n", "").lower()
                p_ovr   = rowP[4]
                p_cmf   = rowP[3]
                p_sty   = rowP[0]
                p_siz   = rowP[8]
                p_arc   = rowP[9]
                if p_arc:
                    p_arc = p_arc[0 : p_arc.index(" ")]     #for arch we only want the first word
                p_wid   = rowP[5]
                p_url   = rowP[2]

                print a_id, p_id

我遇到的问题是,在我转储到 .txt 文件中的输出中,并没有打印 f1 中的所有 product_id。我肯定知道这一点,因为 f1 是我创建的测试文件,我特意在其中放置了几个不同 id 的产品。

要注意的另一件事是,我尝试在单独的脚本中循环遍历每个 csv,并且每个都正常工作,按预期打印出每个 product_id。为什么当我嵌入 for 循环时,迭代第一个文件似乎被缩短了?可能是什么问题呢?我制作的测试文件很小,所以它们应该能够完全适合内存。

【问题讨论】:

  • 为什么将第二个for 循环放在第一个循环中?这可能是您的问题的根源,因为在此循环运行一次后,它将读取到文件的末尾,因此在后续运行中内部循环将不会运行。
  • 将较小的文件读入字典,然后在读取较大的文件时循环遍历。
  • 好的,我试试。谢谢。但我不太明白@BrenBarn 的评论。它不是只读取内部文件的文件结尾,而不读取外部文件的吗?所以不应该继续吗?

标签: python csv


【解决方案1】:

错误是,就像 BrenBam 提到你的循环构造

for row in csv_readerf:
   ....
   for rowP in csv_readern:
       # will only work in the first iteration of the outer loop
       # since the csv reader hits eof
       ...

因此您只需将 csv_readerf 的第一行与 csv_readern 的所有行进行比较

如果您在外循环内打开内循环的 csv 文件,您会阻止这种情况:

for row in csv_readerf:
    ...
    with open(f2, 'rb') as n:
       csv_readern = csv.reader(n)
       csv_readern.next()       ....

        for rowP in csv_readern:
           # will iterate over csv_readern, but only in the first iteration of the outer loop

或者如果你先将内部文件读入一个数组并循环遍历它

这是一个非常常见的初学者错误,经常发生在深度嵌套中,使用函数可能会有所帮助

【讨论】:

    猜你喜欢
    • 2023-01-31
    • 2017-08-06
    • 2020-03-08
    • 2017-07-14
    • 2018-03-07
    • 1970-01-01
    • 2014-08-24
    • 2016-10-18
    • 2012-10-25
    相关资源
    最近更新 更多