0

I have 2 lists of strings I want to compare and highlight the differences between them.

Code snippet:

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"

I want the html file to highlight the differences in the 2 strings. In this case highlight the arfcn.

Kapil
  • 817
  • 2
  • 13
  • 25
Saad Shan
  • 13
  • 4
  • 1
    Hi Saad Shan, please post code allowing us to copy and paste into our own IDE. This allows us to see that you have tried as well as to see what errors you may be having. Thank you:) – Shane Aug 01 '19 at 14:32

1 Answers1

0

It doesn't look like it - make_file() takes lists of strings, not strings: https://docs.python.org/2/library/difflib.html#difflib.HtmlDiff.make_file

You could get around this by using lists, and the resulting diff doesn't look too terrible:

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))
alex
  • 6,818
  • 9
  • 52
  • 103
  • Thanks but what if after each iteration, string1 and string2 change. So I want to continuously write to my Html file. Is it possible to have multiple comparisons in one file? – Saad Shan Aug 01 '19 at 15:09
  • The question does not mention iterations - what are you iterating over? Also, what do you mean by you want to continuously write to the HTML file? And yes you should be able to modify the HTML to display multiple comparisons, I don't see why not. – alex Aug 01 '19 at 15:14