How do I diff utf-16 files with GNU diff?

后端 未结 6 1872
一向
一向 2021-01-01 15:07

GNU diff doesn\'t seem to be smart enough to detect and handle UTF-16 files, which surprises me. Am I missing an obvious command-line option? Is there a good alternative?<

6条回答
  •  再見小時候
    2021-01-01 15:58

    In Python, you can use difflib.HtmlDiff to create an HTML table that shows the differences between two sequences of lines, and it seems to work fine with Unicode strings (provided, of course, you read and write them with the appropriate codecs).

    >>> hd = difflib.HtmlDiff()
    >>> htmldiff = hd.make_file(codecs.open('file1', 'r', 'utf-16').readlines(), codecs.open('file2', 'r', 'utf-16').readlines())
    >>> print >> codecs.open('diff.html', 'w', 'utf-16'), htmldiff
    

提交回复
热议问题