python - 如何将两个文本文件与文件路径进行比较并在另一个文本文件中输出差异?
问题描述
我正在研究一个将两个文本文件相互比较并将它们输出到另一个文本文件的代码。到目前为止,我在第 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')
解决方案
添加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())
推荐阅读
- git - Github 分支策略
- python - 为两个 python 版本(2.7 和 3.7)Windows 10 安装 pip
- android - 收听 Android 4.0+ 的应用安装/卸载(CommonsWare)
- reactjs - 为什么我们不应该在 ComponentDidMount 中设置状态
- javascript - 如何 string.splice 时区
- clojure - 是什么导致 ClojureScript 中的“错误:没有为类型未定义的类型定义的协议方法 XXX.YYY”而不是 Clojure?
- qt - 从 QListWidget Thumbnails 拖动项目后如何获取缩略图的项目索引?
- javascript - Javascript 中的原始类型如何具有方法和属性?
- kotlin - Android Room 中的 AppDatabase 可以随时为空吗?
- python-3.x - 模棱两可的意图和空位填充