Python 3 自动化运维之比较字符串差异difflib模块
2023-09-14 09:16:00 时间
#1.控制台打印输出
#字符串差异比较
import difflib
s1 = """text1:
我是第一个字符串。
我用Python!
and so on.
"""
#以行进行分割
s1_lines = s1.splitlines()
s2 = """text2:
我是第二个字符串。
我用python。
and so on."""
s2_lines = s2.splitlines()
#创建对象
d = difflib.Differ()
#比较
diff = d.compare(s1_lines, s2_lines)
#打印输出
print("\n".join(list(diff)))
#2.生成美观的html文件
d = difflib.HtmlDiff()
html = d.make_file(s1_lines, s2_lines)
print(html)
with open('diff.html', mode='w', encoding='utf-8') as f:
f.write(html)
相关文章
- python中替换字符串中字符_Python replace()函数:替换字符串中的某个字符「建议收藏」
- 【说站】python创建模块的注意点
- python 模块、time、datetime 导入使用(4.0)
- 使用Python获取上海详细疫情数据(一)「建议收藏」
- Python标识符的命名规则,下列哪些是对的?_python标识符不能使用关键字
- python udp编程_Python核心编程
- Python升级之路( Lv16 ) 网络编程
- 使用PYTHON中KERAS的LSTM递归神经网络进行时间序列预测|附代码数据
- Python 冒泡排序_python
- 【测试开发】python系列教程:Python 推导式
- Python中的模块和包
- Python 自动化指南(繁琐工作自动化)第二版:一、PYTHON 基础知识
- 什么是模块,Python模块化编程(入门必读)
- 利用xlrd模块实现Python读取Excel文档详解编程语言
- 一步步学习:利用Python连接MySQL数据库(python连接mysql数据库)
- Python操作MySQL数据库的必备模块mysqlpython(mysql_python)
- 二种python发送邮件实例讲解(python发邮件附件可以使用email模块实现)
- 树莓派中python获取GY-85九轴模块信息示例
- Python删除指定目录下过期文件的2个脚本分享