使用Google diff-match-patch应用部分补丁

时间:2018-10-14 13:22:45

标签: python diff patch google-diff-match-patch

我有一个home.html,该更新频繁,每次我必须重新应用自定义编辑手册时(例如:在.css标记中更改一些<style>规则)。 我已经将完整的<style>部分保存在一个单独的文件modification.txt中。

我希望能够将这两个文件home.hmtlmodification.txt发送到将应用修改的python脚本中。

我注意到google diff-match-patch是否可以做到这一点,但到目前为止,我已经能够比较文件,创建补丁并对其应用补丁。但是该补丁实际上将home.html替换为modification.txt。如何修改我的股票,以便仅替换<style>

import diff_match_patch as dmp_module

def readFileToText(filePath):
    file = open(filePath,"r")
    s = ''
    for line in file:
            s = s + line
    return s


dmp = dmp_module.diff_match_patch()
origin = r"C:\Users\user\Desktop\test\home.html"
lastest = r"C:\Users\user\Desktop\test\modification.txt"

originText = readFileToText(origin)
lastestText = readFileToText(lastest)

patch = dmp.patch_make(originText, lastestText)
patchText = dmp.patch_toText(patch)

finalfile = dmp.patch_apply(patch,originText)
print(finalfile)
patchFilePath  = r"C:\Users\user\Desktop\test\output1.html"
patchFile = open(patchFilePath,"w")
patchFile.write(finalfile[0])

0 个答案:

没有答案