我有三个文件。我想比较列中的水果和匹配的水果,我想将匹配的水果附加到Append.txt文件,然后按升序排序。
test1.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
test2.csv
FRUIT,Amount,Aisle
oranges,1,1
apples,1,1
pears,1,1
Append.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行第1行,第2行第1行,第1行) 2,第2行,等等>)。运行主脚本后,可以填充测试文件,不包含任何记录或最多5条记录。附加文件可以为空或有数百条记录。使用python 2.7。
我也不确定如何在完成后按升序对文件进行排序。
答案 0 :(得分:1)
使用sets。首先阅读两个CSV文件,然后只收集行中的水果。
然后使用set intersectionctions查找两个文件共有的所有水果,将这些水果添加到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)))