如何将差异文本输出到单独的文件中? python3

时间:2018-09-14 08:08:04

标签: python python-3.x compare diff

因此,我尝试制作一个在比较两个文本文件(使用requests.get('url=').text生成的文本文件)后仅显示新行/文本/已添加/删除的文本的机器人。

我知道有difflib。 HtmlDiff(make_file())不适合我,因为它会使文件混乱。另外,据我了解,无论如何,只需要输出一个html文件。然后,您可以在浏览器和Wuala中将其打开!你有比较。

我尝试搜索,但是找不到仅输出已更改/添加的文本的库。

目前我是这样做的:

def htmlCompare(prep_id=None):
while True:
    prep_url = requests.get(prep_template.format(prep_id))
    c1 = prep_url
    c2 = ''
    if c2 != c1:
        compare = difflib.HtmlDiff().make_file(fromlines=c1, tolines=c2, numlines=1)
        c2 = c1
        config_compare_save = open('compare_{}_main.txt'.format(prep_id), 'w')
        config_compare_save.write(compare)
        config_compare_save.close()
    else:  # don't mind this meaningless condition for now, I left this for something for later :)
        time.sleep(10)

但是,正如您所知道的,这并不能满足我的需要。试图使用Differ(),但似乎我不知道如何使用它。甚至不确定那是我首先需要使用的方法。但是据我所知,这与我可能要使用的最接近。

0 个答案:

没有答案