[python] use difflib to compare json differences

I used python to write a method to compare json data before, this time I used the difflib module to achieve:

A json data exists in text1.txt:

 

Another json data exists in text2.txt:

 1. Import the difflib module

import difflib

2. Call the HtmlDiff class in difflib, and use the make_file method to compare the data in the two files

    m = difflib.HtmlDiff()

    result = m.make_file(read_file('/Users/xmly/Desktop/tools/05_jiami/test/logs/text1'),
                         read_file('/Users/xmly/Desktop/tools/05_jiami/test/logs/text2'))
    #判断是否有变化,有变化则生产报告,没有则不生成报告
    if result.count('<span class="diff_sub">') > 0 or result.count('<span class="diff_chg">') > 0 or result.count(
            '<span class="diff_add">') > 0:

        print("find diff")

        with open('/Users/xmly/Desktop/tools/05_jiami/test/logs/result1.html', 'w', encoding='utf-8') as f:

            f.writelines(result)

    else:

        print("not find diff, no html report generate!")

 Output report:

 

Guess you like

Origin blog.csdn.net/weixin_43569834/article/details/131861644