Python 有什么比difflib更强大的替代方案?

Python 有什么比difflib更强大的替代方案?,python,string-comparison,difflib,Python,String Comparison,Difflib,我正在编写脚本,需要能够跟踪修订。一般的想法是给它一个元组列表,其中第一个条目是字段的名称(即“title”或“description”等),第二个条目是该字段的第一个版本,第三个条目是修订版本。比如说: [("Title", "The first version of the title", "The second version of the title")] 现在,使用pythondocx我希望我的脚本创建一个word文件,该文件将显示原始版本,以及带有粗体更改的新版本。例如: 原标题

我正在编写脚本,需要能够跟踪修订。一般的想法是给它一个元组列表,其中第一个条目是字段的名称(即“title”或“description”等),第二个条目是该字段的第一个版本,第三个条目是修订版本。比如说:

[("Title", "The first version of the title", "The second version of the title")]
现在,使用
pythondocx
我希望我的脚本创建一个word文件,该文件将显示原始版本,以及带有粗体更改的新版本。例如:


原标题:

这是标题的第一个版本

修订标题:

这是标题的第二个版本


pythondocx
中实现这一点的方法是创建一个元组列表,其中第一个条目是文本,第二个条目是格式。因此,创建修订标题的方法如下:

paratext = [("This is the ", ''),("second",'b'),(" version of the title",'')]
最近发现了
difflib
,我觉得这是一项非常简单的任务。事实上,对于简单的单词替换,如上面的示例,它是,并且可以通过以下函数完成:

def revFinder(str1,str2):
    s = difflib.SequenceMatcher(None, str1, str2)
    matches = s.get_matching_blocks()[:-1]

    paratext = []

    for i in range(len(matches)):
        print "------"
        print str1[matches[i][0]:matches[i][0]+matches[i][2]]
        print str2[matches[i][1]:matches[i][1]+matches[i][2]]
        paratext.append((str2[matches[i][1]:matches[i][1]+matches[i][2]],''))

        if i != len(matches)-1:
            print ""
            print str1[matches[i][0]+matches[i][2]:matches[i+1][0]]
            print str2[matches[i][1]+matches[i][2]:matches[i+1][1]]
            if len(str2[matches[i][1]+matches[i][2]:matches[i+1][1]]) > len(str1[matches[i][0]+matches[i][2]:matches[i+1][0]]):
                paratext.append((str2[matches[i][1]+matches[i][2]:matches[i+1][1]],'bu'))
            else:
                paratext.append((str1[matches[i][0]+matches[i][2]:matches[i+1][0]],'bu'))

    return paratext
当我想做其他事情时,问题就来了。例如,将“teh”更改为“the”会产生theh(如果没有空格,我无法理解格式)。另一个问题是附加到末尾的额外文本不会显示为更改(或根本不会显示)

所以,我想问大家的问题是,
difflib
有什么替代方法可以处理更复杂的文本比较,或者,我如何更好地使用
difflib
来满足我的需要?提前感谢

为这样的事情做得很好。