python开辟_difflib字符串斗劲

    添加时间:2013-8-6 点击量:

    在python的difflib


    HtmlDiff:斗劲后以html办法显现



    我们斗劲的是字符串:


    hello world! 和 hElLO Wor2d!


    具体代码:



     1  difflib import 
    
    2 import os
    3
    4 def write():
    5 if os.path.exists(E:\\info.html):
    6 with open(E:\\info.htmlw+) as fp:
    7 test = HtmlDiff.make_file(HtmlDiff(), hello world!hElLO Wor2d!
    8 fp.write(test)
    9 print生成文件成功!
    10 fp.close()
    11
    12
    13 def main():
    14 write()
    15
    16 if __name__ == __main__:
    17 main()


    differ



    运行代码:



    1 import difflib
    
    2
    3 test = difflib.Differ().compare(hello worldHeLLO,wOrlD!
    4 print横向显现:
    5 print.join(list(test)))
    6 print 50
    7 test = difflib.Differ().compare(hello worldHeLLO,wOrlD!
    8 print纵向显现:
    9 print\n.join(list(test)))


    SquenceMatcher



    运行代码:



     1 import difflib
    
    2
    3 def test():
    4 test = difflib.SequenceMatcher(lambda x: x == hello worldHeLLO,wOrlD!
    5 for block in test.get_matching_blocks():
    6 printa[%d] and b[%d] match for %d elements block)
    7
    8 def main():
    9 test()
    10
    11 if __name__ == __main__:
    12 main()


    读书,不要想着实用,更不要有功利心。读书只为了自身的修养。邂逅一本好书如同邂逅一位知己,邂逅一个完美之人。有时心生敬意,有时怦然心动。仿佛你心底埋藏多年的话,作者替你说了出来,你们在时光深处倾心相遇的一瞬间,情投意合,心旷神怡。
    分享到: