比较 2 个 csv 文件并使用 Python 2.7 将不同的行输出到第三个 CSV 文件
Compare 2 csv files and output different rows to a 3rd CSV file using Python 2.7
我正在尝试比较两个 csv 文件并使用 python 2.7 查找不同的行。当所有列不相同时,行被认为是不同的。这些文件将采用相同的格式,所有相同的列都将采用这种格式。
oldfile.csv
ID name Date Amount
1 John 6/16/2015 00
2 Adam 6/16/2015 00
newfile.csv
ID name Date Amount
1 John 6/16/2015 00
2 Adam 6/16/2015 00
3 Sam 6/17/2015 00
4 Dan 6/17/2015 00
当我 运行 我的脚本时,我希望输出只是底部两行并写入 csv 文件中,不幸的是我无法让我的代码正常工作。我在下面写的内容打印出 oldfile.csv 的内容,但不打印不同的行。我想让代码做的是打印出 output.csv 文件中的最后几行。即
output.csv
3 Sam 6/17/2015 00
4 Dan 6/17/2015 00
这是我的代码 python 2.7 代码使用 csv 模块。
import csv
f1 = open ("olddata/olddata.csv")
oldFile1 = csv.reader(f1)
oldList1 = []
for row in oldFile1:
oldList1.append(row)
f2 = open ("newdata/newdata.csv")
newFile2 = csv.reader(f2)
newList2 = []
for row in newFile2:
newList2.append(row)
f1.close()
f2.close()
output = [row for row in oldList1 if row not in newList2]
print output
不幸的是代码只打印出oldfile.csv的内容。我整天都在研究它并尝试不同的变体,但我就是无法让它正常工作。再次感谢您的帮助。
您目前正在检查 旧文件中存在但不在新文件中的行。那不是你想做的。
相反,您应该检查新文件中存在但不在新文件中的行:
output = [row for row in newList2 if row not in oldList1]
此外,您的 CSV 文件是 TSV,因此无法正确加载。您应该指示 csv
模块使用 TSV 打开您的文件。您的代码也可以简化。
以下是您可以使用的内容:
import csv
f1 = open ("olddata/olddata.csv")
oldFile1 = csv.reader(f1, delimiter='\t')
oldList1 = list(oldFile1)
f2 = open ("newdata/newdata.csv")
newFile2 = csv.reader(f2, delimiter='\t')
newList2 = list(newFile2)
f1.close()
f2.close()
output1 = [row for row in newList2 if row not in oldList1]
output2 = [row for row in oldList1 if row not in newList2]
print output1 + output2
如果您的文件看起来像提供的输入,则可以使用集合:
with open("olddata/olddata.csv") as f1, open("newdata/newdata.csv") as f2:
header = next(f1).split()
st = set(f1)
with open("out.csv","w") as out:
wr = csv.writer(out,delimter="\t")
# write lines only if they are not in the set of lines from olddata/olddata.csv
wr.writerows((row.split() for row in f2 if row not in st))
您无需在 newdata.csv
中创建行列表,您可以遍历文件对象并随心所欲地编写或执行任何操作。另外 with
将自动关闭您的文件。
或者没有 csv 模块,只存储行:
with open("olddata/olddata.csv") as f1, open("newdata/newdata.csv") as f2:
header = next(f1)
st = set(f1)
with open("out.csv", "w") as out:
out.writelines((line for line in f2 if line not in st))
输出:
ID name Date Amount
3 Sam 6/17/2015 00
4 Dan 6/17/2015 00
或者使用 csv 模块完成所有操作:
import csv
from itertools import imap
with open("olddata/olddata.csv") as f1, open("newdata/newdata.csv") f2:
r1 = csv.reader(f1, delimiter="\t")
header = next(r1)
st = set(imap(tuple, r1))
with open("out.csv", "w") as out:
wr = csv.writer(out, delimiter="\t")
r2 = csv.reader(f2, delimiter="\t")
wr.writerows((row for row in imap(tuple, f2) if row not in st))
如果您不关心顺序并且想要出现在其中一个但不同时出现在两者中的行,您可以使用 set.symmetric_difference.
import csv
from itertools import imap
with open("olddata/olddata.csv") as f1, open("newdata/newdata.csv") f2:
r1 = csv.reader(f1, delimiter="\t")
header = next(r1)
st = set(imap(tuple, r1))
r2 = csv.reader(f2, delimiter="\t")
print(st.symmetric_difference(imap(tuple, r2)))
输出:
set([('ID', '', 'name', 'Date', 'Amount'), ('3', 'Sam', '6/17/2015', '00'), ('4', 'Dan', '6/17/2015', '00')])
排序数据和写入仍然比使用列表更有效。
我正在尝试比较两个 csv 文件并使用 python 2.7 查找不同的行。当所有列不相同时,行被认为是不同的。这些文件将采用相同的格式,所有相同的列都将采用这种格式。
oldfile.csv
ID name Date Amount
1 John 6/16/2015 00
2 Adam 6/16/2015 00
newfile.csv
ID name Date Amount
1 John 6/16/2015 00
2 Adam 6/16/2015 00
3 Sam 6/17/2015 00
4 Dan 6/17/2015 00
当我 运行 我的脚本时,我希望输出只是底部两行并写入 csv 文件中,不幸的是我无法让我的代码正常工作。我在下面写的内容打印出 oldfile.csv 的内容,但不打印不同的行。我想让代码做的是打印出 output.csv 文件中的最后几行。即
output.csv
3 Sam 6/17/2015 00
4 Dan 6/17/2015 00
这是我的代码 python 2.7 代码使用 csv 模块。
import csv
f1 = open ("olddata/olddata.csv")
oldFile1 = csv.reader(f1)
oldList1 = []
for row in oldFile1:
oldList1.append(row)
f2 = open ("newdata/newdata.csv")
newFile2 = csv.reader(f2)
newList2 = []
for row in newFile2:
newList2.append(row)
f1.close()
f2.close()
output = [row for row in oldList1 if row not in newList2]
print output
不幸的是代码只打印出oldfile.csv的内容。我整天都在研究它并尝试不同的变体,但我就是无法让它正常工作。再次感谢您的帮助。
您目前正在检查 旧文件中存在但不在新文件中的行。那不是你想做的。
相反,您应该检查新文件中存在但不在新文件中的行:
output = [row for row in newList2 if row not in oldList1]
此外,您的 CSV 文件是 TSV,因此无法正确加载。您应该指示 csv
模块使用 TSV 打开您的文件。您的代码也可以简化。
以下是您可以使用的内容:
import csv
f1 = open ("olddata/olddata.csv")
oldFile1 = csv.reader(f1, delimiter='\t')
oldList1 = list(oldFile1)
f2 = open ("newdata/newdata.csv")
newFile2 = csv.reader(f2, delimiter='\t')
newList2 = list(newFile2)
f1.close()
f2.close()
output1 = [row for row in newList2 if row not in oldList1]
output2 = [row for row in oldList1 if row not in newList2]
print output1 + output2
如果您的文件看起来像提供的输入,则可以使用集合:
with open("olddata/olddata.csv") as f1, open("newdata/newdata.csv") as f2:
header = next(f1).split()
st = set(f1)
with open("out.csv","w") as out:
wr = csv.writer(out,delimter="\t")
# write lines only if they are not in the set of lines from olddata/olddata.csv
wr.writerows((row.split() for row in f2 if row not in st))
您无需在 newdata.csv
中创建行列表,您可以遍历文件对象并随心所欲地编写或执行任何操作。另外 with
将自动关闭您的文件。
或者没有 csv 模块,只存储行:
with open("olddata/olddata.csv") as f1, open("newdata/newdata.csv") as f2:
header = next(f1)
st = set(f1)
with open("out.csv", "w") as out:
out.writelines((line for line in f2 if line not in st))
输出:
ID name Date Amount
3 Sam 6/17/2015 00
4 Dan 6/17/2015 00
或者使用 csv 模块完成所有操作:
import csv
from itertools import imap
with open("olddata/olddata.csv") as f1, open("newdata/newdata.csv") f2:
r1 = csv.reader(f1, delimiter="\t")
header = next(r1)
st = set(imap(tuple, r1))
with open("out.csv", "w") as out:
wr = csv.writer(out, delimiter="\t")
r2 = csv.reader(f2, delimiter="\t")
wr.writerows((row for row in imap(tuple, f2) if row not in st))
如果您不关心顺序并且想要出现在其中一个但不同时出现在两者中的行,您可以使用 set.symmetric_difference.
import csv
from itertools import imap
with open("olddata/olddata.csv") as f1, open("newdata/newdata.csv") f2:
r1 = csv.reader(f1, delimiter="\t")
header = next(r1)
st = set(imap(tuple, r1))
r2 = csv.reader(f2, delimiter="\t")
print(st.symmetric_difference(imap(tuple, r2)))
输出:
set([('ID', '', 'name', 'Date', 'Amount'), ('3', 'Sam', '6/17/2015', '00'), ('4', 'Dan', '6/17/2015', '00')])
排序数据和写入仍然比使用列表更有效。