我有三个文件。我想比较其中有水果的列和匹配的列,我想将匹配的水果附加到 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。
我也不确定如何在完成后按升序对文件进行排序。