我试图获取两个csv文件A.csv和B.csv之间的区别,以获取在第二个文件中添加的新行。 A.csv具有以下数据。
acct ABC 88888888 99999999 ABC-GHD 4/1/18 4 1 2018 Redundant/RSK
B.csv有以下数据。
acct ABC 88888888 99999999 ABC-GHD 4/1/18 4 1 2018 Redundant/RSK
acct ABC 88888888 99999999 ABC-GHD 4/1/18 4 1 2018 DT/89
使用以下脚本编写添加到输出文件中的新行。
input_file1 = "A.csv"
input_file2 = "B.csv"
output_path = "out.csv"
with open(input_file1, 'r') as t1:
fileone = set(t1)
with open(input_file2, 'r') as t2, open(output_path, 'w') as outFile:
for line in t2:
if line not in fileone:
outFile.write(line)
预期输出为:
acct ABC 88888888 99999999 ABC-GHD 4/1/18 4 1 2018 DT/89
通过上述脚本获得的输出是:
acct ABC 88888888 99999999 ABC-GHD 4/1/18 4 1 2018 Redundant/RSK
acct ABC 88888888 99999999 ABC-GHD 4/1/18 4 1 2018 DT/89
我不确定我在哪里犯了错误,尝试调试它但没有任何进展。
答案 0 :(得分:2)
您需要小心尾随换行符。因此,在比较之前删除换行更安全,然后在写入时将其添加回来:
input_file1 = "A.csv"
input_file2 = "B.csv"
output_path = "out.csv"
with open(input_file1, 'r') as t1:
fileone = set(t1.read().splitlines())
with open(input_file2, 'r') as t2, open(output_path, 'w') as outFile:
for line in t2:
line = line.strip()
if line not in fileone:
outFile.write(line + '\n')