首页 > 解决方案 > 如何将两个文本文件与文件路径进行比较并在另一个文本文件中输出差异?

问题描述

我正在研究一个将两个文本文件相互比较并将它们输出到另一个文本文件的代码。到目前为止,我在第 5 行中不断收到“不可调用的 unicode”错误。我现在的代码中缺少什么?

enter code here
import glob, os, shutil, time, string

def compare(File1,File2):
    with open(File1,'r') as f:
        d=set(f.readlines())


    with open(File2,'r') as f:
         e=set(f.readlines())


    with open('C:\...\results.txt','a') as f:
        for line in list(d-e):
           f.write(line)




compare(r'C:\...\original_contours.txt',r'C:\...\reprojected_contours.txt')

标签: python

解决方案


添加encoding='utf8'到您的代码中:

with open(File1, 'r', encoding='utf8') as f:
    d=set(f.readlines())

...

with open(File2, 'r', encoding='utf8') as f:
     e=set(f.readlines())

推荐阅读