首页 > 解决方案 > 字符串可以用来生成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。

标签: pythonhtmldifflib

解决方案


它看起来不像 -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))

推荐阅读