我试图获得两个csv文件A.csv和B.csv之间的差异,以便获得在第二个文件中添加的新行。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
我不知道我在哪里犯了错误,尝试调试它,但没有任何进展
你需要小心后面的换行符。因此,在比较之前删除换行符,然后在写入时将其添加回更安全的方法是:
相关问题 更多 >
编程相关推荐