我有下面的格式2个CSV文件:Python的比较两个CSV文件,并且将数据追加到csv文件
首先是outputTweetsDate.csv:
Here is some text;13.09.13 16:45
Here is more text;13.09.13 16:45
And yet another text;13.09.13 16:46
第二个文件是apiSheet.csv :
13.09.13 16:46;89.56
13.09.13 16:45;90.40
我想这两个文件进行比较,如果两个日期时间值匹配的文本和数据添加到一个新文件(finalOutput.csv):
|89.56|,|Here is some text|
|89.56|,|Here is more text|
|90.49|,|And yet another text|
这是我的代码,我到目前为止有:
with open("apiSheet.csv", "U") as in_file1, open("outputTweetsDate.csv", "rb") as in_file2,open("finalOutput.csv", "wb") as out_file:
reader1 = csv.reader(in_file1,delimiter=';')
reader2 = csv.reader(in_file2,delimiter='|')
writer = csv.writer(out_file,delimiter='|')
for row1 in reader1:
for row2 in reader2:
if row1[0] == row2[1]:
data = [row1[1],row2[0]]
print data
writer.writerow(data)
我修改了代码,它现在的作品,到目前为止,但它并不低谷遍历所有代码正确。 瞬间我的输出是这样的:
|89.56|,|Here is some text|
|89.56|,|Here is more text|
所以它不显示我的第三个,即使它们是相同的。它似乎不是通过文件迭代。
谢谢!
行是按时间顺序排列的吗?或者,file1中第一行对应的某个日期是否在file2的最后一行(或者根本没有?) –
如果时间戳是唯一的,我将使用timestamp作为key并合并它们。 –
@Ofir yes行按时间顺序排列 – Max