python - 字符串可以用来生成difflib.HtmlDiff().make_file的比较报告吗?
问题描述
我有 2 个要比较并突出显示它们之间差异的字符串列表。
代码片段:
string1 = "GNBDUFunction=1,TddRadioChannel=1 arfcn 632333, channelBandwidth 20000, frequency , reservedBy [1] = , >>> reservedBy = GNBDUFunction=1,GNodeBSectorCarrier=T23MGNX, tddRadioChannelId 1"
string2 = "GNBDUFunction=1,TddRadioChannel=1 arfcn 633333, channelBandwidth 20000, frequency 37000080, reservedBy [1] = , >>> reservedBy = GNBDUFunction=1,GNodeBSectorCarrier=TESTNX, tddFrequency 0, tddRadioChannelId 1"
我希望 html 文件突出显示 2 个字符串中的差异。在这种情况下,突出显示 arfcn。
解决方案
它看起来不像 -make_file()
需要字符串列表,而不是字符串:https ://docs.python.org/2/library/difflib.html#difflib.HtmlDiff.make_file
您可以通过使用列表来解决这个问题,并且生成的差异看起来并不太糟糕:
string_list_1 = "GNBDUFunction=1,TddRadioChannel=1 arfcn 632333, channelBandwidth 20000, frequency , reservedBy [1] = , >>> reservedBy = GNBDUFunction=1,GNodeBSectorCarrier=T23MGNX, tddRadioChannelId 1".split(",")
string_list_2 = "GNBDUFunction=1,TddRadioChannel=1 arfcn 633333, channelBandwidth 20000, frequency 37000080, reservedBy [1] = , >>> reservedBy = GNBDUFunction=1,GNodeBSectorCarrier=TESTNX, tddFrequency 0, tddRadioChannelId 1".split(",")
print(difflib.HtmlDiff().make_file(strings1, strings2))
推荐阅读
- reactjs - 在 ComponentDidMount 中更新状态后,React 的值状态为空
- .net - .NET 中计算冲突相对较少的长字符串的短哈希字符串的好方法是什么?
- python - 在 PySide2 中设置 QListWidget 流的语法是什么?
- amazon-web-services - Ansible - 从最近的现有快照创建 RDS 实例
- spring - Spring Cron 表达式 - 未按计划运行
- javascript - 自定义菜单未在 Electron 中显示 - MacOS
- javascript - 如何将数据传递给 Ant Design Modal
- java - 如何仅在键入所有输入后打印输出,而不是逐行打印?
- templates - 如何在ckeditor中放置页眉和页脚
- java - 必需:变量、找到的值(数组故障排除)