【发布时间】:2015-06-15 18:43:10
【问题描述】:
我正在尝试比较 python 中的两个 csv 文件并将差异保存到 python 2.7 中的第三个 csv 文件。
import csv
f1 = open ("olddata/file1.csv")
oldFile1 = csv.reader(f1)
oldList1 = []
for row in oldFile1:
oldList1.append(row)
f2 = open ("newdata/file2.csv")
oldFile2 = csv.reader(f2)
oldList2 = []
for row in oldFile2:
oldList2.append(row)
f1.close()
f2.close()
set1 = tuple(oldList1)
set2 = tuple(oldList2)
print oldList2.difference(oldList1)
我收到错误消息:
Traceback (most recent call last):
File "compare.py", line 21, in <module>
print oldList2.difference(oldList1)
AttributeError: 'list' object has no attribute 'difference'
我是 python 的新手,一般是编码,我还没有完成这段代码(我必须确保将差异存储到变量并将差异写入新的 csv 文件。)。我整天都在尝试解决这个问题,但我根本做不到。您的帮助将不胜感激。
【问题讨论】:
-
您将
list与set混淆了
标签: python python-2.7 csv compare