0

我有三个文件。我想比较其中有水果的列和匹配的列,我想将匹配的水果附加到 Append.txt 文件中,然后升序排序。

测试1.csv

CustID,Name,Count,Item,Date  
23,Smith,8,apples,08/12/2010  
1,Jones,8,banana,03/26/2009  
15,Miller,2,cookie dough,03/27/2009  
6,Fisher,8,oranges,06/09/2011  

测试2.csv

FRUIT,Amount,Aisle  
oranges,1,1  
apples,1,1  
pears,1,1  

追加.txt

Fruit,Total,Aisle
cherries,1,1  
dates,2,1  
grapes,5,1  
kiwis,2,2  
peaches,2,2  
plums,1,1  
watermelon1,2  

代码:

import csv

# Iterate through both  reader1 and reader2, compare common row, and append matching column data to test.txt in its matching column
with open("C:\\Test\\Append.txt", 'a') as f:
    reader1 = csv.reader(open("C:\\Test\\test1.csv", 'rb'), delimiter=',')
    row1 = reader1.next()
    reader2 = csv.reader(open("C:\\Test\\test2.csv", 'rb'), delimiter=',')
    row2 = reader2.next()
    if (row1[3] == row2[0]):
        print "code to append data from row1[0] to test.txt row[0] goes here"

f.close()
exit

print "code to sort test.txt ascending on column[0] goes here"

我的初始脚本不起作用。检查后我可以看到代码仅将第 1 行与第 1 行、第 2 行与第 2 行等进行比较,我真的希望它比较所有行(第 1 行与第 1 行、第 1 行与第 2 行、第 2 行与第 1 行、第2 与第 2 行等>)。运行主脚本后,可以填充没有记录或最多 5 条记录的测试文件。附加文件可以是空的,也可以有数百条记录。使用python 2.7。

我也不确定如何在完成后按升序对文件进行排序。

4

1 回答 1

1

使用集合。首先读取两个 CSV 文件,然后只收集行中的结果。

然后使用集合交集找到两个文件共有的所有水果,将它们添加到Append.txt文件中的水果,排序,然后将所有水果写回文件。

import csv

# collect the fruits of both CSV files
with open('c:/Test/test1.csv', 'rb') as test1:
    reader = csv.reader(test1)
    next(reader, None)  # ignore header
    test1_fruit = set(row[3] for row in reader)
with open('c:/Test/test2.csv', 'rb') as test2:
    reader = csv.reader(test2)
    next(reader, None)  # ignore header
    test2_fruit = set(row[0] for row in reader)

# Read all the fruit from Append
with open("C:/Test/Append.txt", 'r') as append:
    fruit = set(line.strip() for line in append if line.strip())

# add all fruit that are in both test1 and test2
fruit |= test1_fruit & test2_fruit

# write out a sorted list
with open("C:/Test/Append.txt", 'w') as append:
    append.write('\n'.join(sorted(fruit)))
于 2013-03-28T20:42:39.113 回答