Python 问题:计算两个csv文件之间的差异
我试图获得两个csv文件A.csv和B.csv之间的差异,以便获得在第二个文件中添加的新行。A.csv包含以下数据Python 问题:计算两个csv文件之间的差异,python,csv,Python,Csv,我试图获得两个csv文件A.csv和B.csv之间的差异,以便获得在第二个文件中添加的新行。A.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 Redundant/RSK acct ABC 888
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 Redundant/RSK
acct ABC 88888888 99999999 ABC-GHD 4/1/18 4 1 2018 DT/89
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 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
我不确定我在哪里犯了错误,尝试调试它,但没有任何进展 您需要小心尾部换行符。因此,在比较之前删除换行符,然后在写入时将其添加回更安全:
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')
您需要小心尾随的换行符。因此,在比较之前删除换行符,然后在写入时将其添加回更安全:
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')
您的第一个文件没有尾随换行符<代码>“…”!=“\n'感谢您识别问题。您的第一个文件没有尾随换行符<代码>“…”!=“\n'感谢您确定问题所在。