Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 问题:计算两个csv文件之间的差异_Python_Csv - Fatal编程技术网

Python 问题:计算两个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

我试图获得两个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     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'感谢您确定问题所在。