python实现:去除空格和换行符,比较两个text文件文本内容的差异,并输出第一个差异值
# coding: utf-8#准备工作,把生成的t2.text文件通过Notepad++转成utf-8编码#输出结果为第一个存在差异的数据#每修改一次t2.text文件需要进行新一次转码import osos.chdir("D:/tesseract/t2")#更改文件目录#os.system("dir")#python 3.x#python 对比两个文件内容with open("t2.txt","r",encoding="utf-8") as file1,\open("text.txt","r",encoding="utf-8") as file2: fa = file1.read() fb = file2.read() fa =fa.replace(" ","") fa =fa.replace("\n","") fb =fb.replace(" ","") fb =fb.replace("\n","") sta=len(fa) stb=len(fb)if sta!=stb: print("错误,长度不相等")for m in range(sta): if fa[m]==fb[m]: flag=-1 else: flag = 1 print(fa[m]) print(fb[m]) breakif flag==1: print('数据存在差异')elif flag==-1: