前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >python使用difflib对比文件示

python使用difflib对比文件示

作者头像
py3study
发布2020-01-06 18:28:47
1.3K0
发布2020-01-06 18:28:47
举报
文章被收录于专栏:python3python3

使用difflib模块对比文件内容

1  示例:字符串差异对比 

vim duibi.py

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import re
import os
import difflib
tex1="""tex1:
this is a test for difflib ,just try to get difference of the log
现在试试功能是否可行 好呀
goodtest
那么试试吧好人
"""
tex1_lines=tex1.splitlines()
tex2="""tex2:
this is a test for difflib ,just try to get difference of the log
现在试试功能是否可行
goodtast
那么试试吧
"""
tex2_lines=tex2.splitlines()
#---------原始对比方法----------
#d=difflib.Differ()
#diff=d.compare(tex1_lines,tex2_lines)
#print '\n'.join(list(diff))

#--------html对比方法----------
#并修改diff.html的编码,将ISO-8859-1改为UTF-8格式解析文件,用于对比中文
d=difflib.HtmlDiff()
q=d.make_file(tex1_lines,tex2_lines)
old_str='charset=ISO-8859-1'
new_str='charset=UTF-8'
with open('diff.html','w') as f_new:
	f_new.write(q.replace(old_str,new_str))

#############################
#d=difflib.HtmlDiff()
#q=d.make_file(tex1_lines,tex2_lines)
#old_str='charset=ISO-8859-1'
#new_str='charset=UTF-8'
#data=q.replace(old_str,new_str)
#fo=open('diff.html','w')
#fo.write(data)
#fo.close()
############################

运行 python duibi.py  生产diff.html

浏览器打开diff.html  查看对比结果。

image.png
image.png

2 示例 文件对比  文件差异对比代码 可直接使用 无需修改(包括中文)

用下面脚本对比  testfile1 testfile2 的差异   

vim diff.py

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import sys
import difflib

try:
	tfile1=sys.argv[1]
	tfile2=sys.argv[2]
except Exception,e:
	print "错误:"+str(e)
	print "请准确输入参数,例如:python diff.py file1 file2"
	sys.exit()
def readfile(filename):
	try:
		fileHandle=open(filename,'rb')
		lines=fileHandle.read().splitlines()
		fileHandle.close()
		return lines
	except IOError as error:
		print('读取文件错误:'+str(error))
		sys.exit()
if tfile1=="" or tfile2=="":
	print "请准确输入参数,例如:python diff.py file1 file2"
	sys.exit()
	
tfile1_lines=readfile(tfile1)
tfile2_lines=readfile(tfile2)

#d=difflib.HtmlDiff()
#print s.make_file(tfile1_lines,tfile2_lines)

#为了生成html能识别中文,可用下面代码 #修改diff.html的编码,将ISO-8859-1改为UTF-8
#====================================
#方法1:
#d=difflib.HtmlDiff()
#q=d.make_file(tfile1_lines,tfile2_lines)
#old_str='charset=ISO-8859-1'
#new_str='charset=UTF-8'
#data=q.replace(old_str,new_str)
#fo=open('diff.html','w')
#fo.write(data)
#fo.close()
#====================================
#方法2:
#d=difflib.HtmlDiff()
#q=d.make_file(tfile1_lines,tfile2_lines)
#old_str='charset=ISO-8859-1'
#new_str='charset=UTF-8'
#fo=open('diff.html','w')
#fo.write(q)
#fo.close()
#with open('diff.html','r') as f:
#        lines=f.readlines()
#with open('diff.html','w') as f_new:
#        for line in lines:
#                f_new.write(line.replace(old_str,new_str))
#=====================================
#方法3:
old_str='charset=ISO-8859-1'
new_str='charset=UTF-8'
d=difflib.HtmlDiff()
q=d.make_file(tfile1_lines,tfile2_lines)
with open('diff.html','w') as f_new:
	f_new.write(q.replace(old_str,new_str))

执行python diff.py testfile1 testfile2    

生成diff.html

浏览器查看文件对比结果

image.png
image.png
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-09-17 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档