比较Python中的两个CSV文件

时间:2015-08-07 01:02:53

标签: python-2.7 csv for-loop nested-loops

我有两个CSV文件,如下所示:

CSV1:

**ID  Name  Address  Ph**
  1   Mr.C   dsf     142
  2   Ms.N   asd     251
  4   Mr.V   fgg     014
  12  Ms.S   trw     547

CSV2:

**ID  Name  Service  Day**
  1   Mr.C   AAA     Mon
  2   Ms.N   AAA     Mon
  2   Ms.N   BBB     Tue
  2   Ms.N   AAA     Sat

正如您可以很快看到的那样,CSV1文件是唯一的,每个ID只有一个实例,而CSV2重复。

我正在尝试根据ID匹配两个CSV文件,然后在匹配的任何位置向CSV2文件添加CSV1中的地址和Ph字段。然后将其保存为新的输出文件,保留两个原始CSV文件。

我已经编写了一段代码,但这里发生了什么:

  1. CSV1的所有条目都会添加到CSV2的最后一行
  2. 或者CSV2中的所有条目都会获得相同的地址详细信息
  3. 这是我到目前为止所做的事情。

    import csv
    csv1=open('C:\csv1file.csv')
    csv2=open('C:\csv2file.csv')
    csv1reader=csv.reader(csv1)
    csv2reader=csv.reader(csv2)
    
    outputfile=open('C:\mapped.csv', 'wb')
    csvwriter=csv.writer(outputfile)
    
    counter=0
    header1=csv1reader.next()
    header2=csv2reader.next()
    
    csvwriter.writerow(header2+header1[2:4])
    
    for row1 in csv1reader:
        for row2 in csv2reader:
            if row1[0]==row2[0]:
                counter=counter+1
            csvwriter.writerow(row2+row1[2:4])
    

    我在Python 2.7中运行此代码。您可能已经猜到了我得到的两个不同的结果是基于上面代码中csvwriter语句的缩进。我觉得我非常接近答案并理解逻辑,但不知何故循环不能很好地循环。

    你们中的任何人都可以帮忙吗?

    感谢。

1 个答案:

答案 0 :(得分:2)

The problem arises because the inner loop only works once. the reason for that is, because csv2reader will be empty after you run the loop once

a way to fix this would be to make a copy of the rows in the second file and use that copy in the loop

csvwriter.writerow(header2+header1[2:4])

csv2copy=[]
for row2 in csv2reader: csv2copy.append(row2)

for row1 in csv1reader:
    for row2 in csv2copy:
        print row1,row2,counter
        if row1[0]==row2[0]:
            counter=counter+1
            csvwriter.writerow(row2+row1[2:4])
相关问题