使用Python进行文本信息的比较并生成HTML报告
2017-04-17 07:47
756 查看
使用Python可以实现类似Linux中diff的命令,还能够输出HTML文档,这是我一直期待的。如果办公环境十分封闭,使用diff命令都难的时候,这无疑是很好的补充。
Python中的标准库有一个difflib,借助于这个库就能够实现所需的比较。
两个文件分别如下:
d1.py
1 from IPyimport IP
2
3 ip1 = IP('192.168.1.2')
4 ip2 = IP('11.12.13.14')
5
6 print("ip1 type: %s" % ip1.iptype())
7 print("ip2 type: %s" % ip2.iptype())
8
9 print("ip2 int value: %d" % ip2.int())
10 print("ip2 hex value: %s" % ip2.strHex())
11 print("ip2 bin value: %s" % ip2.strBin())
12
13 print("IP for 0x1234567: %s" % IP(0x1234567))
14
d2.py
1 from IPyimport IP
2
3 ip1 = IP('192.168.1.2')
4 ip2 = IP('11.33.13.14')
5
6 print("ip1 type: %s" % ip1.iptype())
7 print("ip2 type: %s" % ip2.iptype())
8
9 print("ip2 int value: %d" % ip1.int())
10 print("ip2 hex value: %s" % ip1.strHex())
11 print("ip2 bin value: %s" % ip1.strBin())
12
13 print("IP for 0x1234567: %s" % IP(0x1234567))
14
比较结果如下:
GreydeMac-mini:Chapter_02 greyzhang$ python pydiff.py
from IPy import IP
ip1 = IP('192.168.1.2')
- ip2 = IP('11.12.13.14')
? ^^
+ ip2 = IP('11.33.13.14')
? ^^
print("ip1 type: %s" % ip1.iptype())
print("ip2 type: %s" % ip2.iptype())
- print("ip2 int value: %d" % ip2.int())
? ^
+ print("ip2 int value: %d" % ip1.int())
? ^
- print("ip2 hex value: %s" % ip2.strHex())
? ^
+ print("ip2 hex value: %s" % ip1.strHex())
? ^
- print("ip2 bin value: %s" % ip2.strBin())
? ^
+ print("ip2 bin value: %s" % ip1.strBin())
? ^
print("IP for 0x1234567: %s" % IP(0x1234567))
差异被顺利比较出来,最终展示的格式也全都跟Linux的diff命令类似。
借用这个模块库还能够生成HTML文档,把前面的代码做一下修改如下:
1 import difflib
2
3 text1 =
open('d1.py').readlines()
4 text2 =
open('d2.py').readlines()
5
6 d = difflib.HtmlDiff()
7 fid =open('report.html','w')
8 fid.write(d.make_file(text1,text2))
9 fid.close()
程序运行后生成了一个html文件,运行的结果以及信息如下:
GreydeMac-mini:Chapter_02 greyzhang$ python pydiff.py
GreydeMac-mini:Chapter_02 greyzhang$ ls
c01.py d2.pypydiff.py
d1.py dns_parser.pyreport.html
使用浏览器打开生成的报告文件如下:
这报告的观感确实是非常好,看多了命令行再看这个感觉简直是精美绝伦!
Python中的标准库有一个difflib,借助于这个库就能够实现所需的比较。
两个文件分别如下:
d1.py
1 from IPyimport IP
2
3 ip1 = IP('192.168.1.2')
4 ip2 = IP('11.12.13.14')
5
6 print("ip1 type: %s" % ip1.iptype())
7 print("ip2 type: %s" % ip2.iptype())
8
9 print("ip2 int value: %d" % ip2.int())
10 print("ip2 hex value: %s" % ip2.strHex())
11 print("ip2 bin value: %s" % ip2.strBin())
12
13 print("IP for 0x1234567: %s" % IP(0x1234567))
14
d2.py
1 from IPyimport IP
2
3 ip1 = IP('192.168.1.2')
4 ip2 = IP('11.33.13.14')
5
6 print("ip1 type: %s" % ip1.iptype())
7 print("ip2 type: %s" % ip2.iptype())
8
9 print("ip2 int value: %d" % ip1.int())
10 print("ip2 hex value: %s" % ip1.strHex())
11 print("ip2 bin value: %s" % ip1.strBin())
12
13 print("IP for 0x1234567: %s" % IP(0x1234567))
14
比较结果如下:
GreydeMac-mini:Chapter_02 greyzhang$ python pydiff.py
from IPy import IP
ip1 = IP('192.168.1.2')
- ip2 = IP('11.12.13.14')
? ^^
+ ip2 = IP('11.33.13.14')
? ^^
print("ip1 type: %s" % ip1.iptype())
print("ip2 type: %s" % ip2.iptype())
- print("ip2 int value: %d" % ip2.int())
? ^
+ print("ip2 int value: %d" % ip1.int())
? ^
- print("ip2 hex value: %s" % ip2.strHex())
? ^
+ print("ip2 hex value: %s" % ip1.strHex())
? ^
- print("ip2 bin value: %s" % ip2.strBin())
? ^
+ print("ip2 bin value: %s" % ip1.strBin())
? ^
print("IP for 0x1234567: %s" % IP(0x1234567))
差异被顺利比较出来,最终展示的格式也全都跟Linux的diff命令类似。
借用这个模块库还能够生成HTML文档,把前面的代码做一下修改如下:
1 import difflib
2
3 text1 =
open('d1.py').readlines()
4 text2 =
open('d2.py').readlines()
5
6 d = difflib.HtmlDiff()
7 fid =open('report.html','w')
8 fid.write(d.make_file(text1,text2))
9 fid.close()
程序运行后生成了一个html文件,运行的结果以及信息如下:
GreydeMac-mini:Chapter_02 greyzhang$ python pydiff.py
GreydeMac-mini:Chapter_02 greyzhang$ ls
c01.py d2.pypydiff.py
d1.py dns_parser.pyreport.html
使用浏览器打开生成的报告文件如下:
这报告的观感确实是非常好,看多了命令行再看这个感觉简直是精美绝伦!
相关文章推荐
- 如何利用深度学习写诗歌(使用Python进行文本生成)
- 使用Python实现一个文本对比报告生成工具
- 使用python生成二维码的HTML文本
- Python之使用unittest框架和HTMLTestRunner.py文件实现多个测试用例执行与测试报告生成
- python使用 HTMLTestRunner.py生成测试报告
- Python 使用 HTMLTestRunner.py 生成测试报告
- 如何利用深度学习写诗歌(使用Python进行文本生成)
- Python 文本挖掘:使用gensim进行文本相似度计算
- Python学习之使用Python生成PDF报告
- Python 使用nltk和BeautifulSoup进行数据清理 (去除html tag和转换html entities)
- Beyond Compare脚本:命令行批量比较文件并生成html格式的差异报告
- Python我入门了,使用Python生成文本还是挺方便的
- Beyond Compare脚本:比较文件并生成html格式的差异报告
- python 使用CppHeaderParser库 根据c++头文件生成html说明文档表格
- 黄聪:使用Python中的HTMLParser、cookielib抓取和解析网页、从HTML文档中提取链接、图像、文本、Cookies(二)
- Python 文本挖掘:使用gensim进行文本相似度计算 http://rzcoding.blog.163.com/blog/static/2222810172013101895642665/
- Beyond Compare脚本:比较文件并生成html格式的差异报告
- python自动生成易于阅读的html文档——使用Sphinx
- python进行文本处理生成redmine表格
- 使用Python中的HTMLParser、cookielib抓取和解析网页、从HTML文档中提取链接、图像、文本、Cookies(二)(转)