在python的difflib

HtmlDiff:比较后以html方法展示

我们比较的是字符串:

'hello world!' 和 'hElLO Wor2d!'

具体代码:

 from difflib import *
import os def write():
if os.path.exists('E:\\info.html'):
with open('E:\\info.html','w+') as fp:
test = HtmlDiff.make_file(HtmlDiff(), 'hello world!', 'hElLO Wor2d!')
fp.write(test)
print('生成文件成功!')
fp.close() def main():
write() if __name__ == '__main__':
main()

differ

运行代码:

 import difflib

 test = difflib.Differ().compare('hello world', 'HeLLO,wOrlD!')
print('横向展示:')
print(''.join(list(test)))
print('#' * 50)
test = difflib.Differ().compare('hello world', 'HeLLO,wOrlD!')
print('纵向展示:')
print('\n'.join(list(test)))

SquenceMatcher

运行代码:

 import difflib

 def test():
test = difflib.SequenceMatcher(lambda x: x == " ", 'hello world', 'HeLLO,wOrlD!')
for block in test.get_matching_blocks():
print("a[%d] and b[%d] match for %d elements" % block) def main():
test() if __name__ == '__main__':
main()

python开发_difflib字符串比较的更多相关文章

  1. Python 开发基础-字符串类型讲解(字符串方法)-2

    s = 'Hello World!'print(s.index('W',0,9))#返回某个字母的索引值,本例返回6.没有该字母会报错,和FIND比较像,find不会报错,没找到会返回-1print( ...

  2. Python 开发基础-字符串类型讲解(字符串方法)-1

    s = 'Hello World!' print(s.capitalize()) #第一个字母大写,其余小写# 输出:Hello world!print(s.swapcase())#大写变小写,小写变 ...

  3. Python开发【字符串格式化篇】

    1.百分号 __author__ = "Tang" # + 号 拼接 msg = "i am " + " tang" print(msg) ...

  4. python开发学习-day01 (python安装与版本、字符串、字典、运算符、文件)

    *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !important; } /* ...

  5. python开发_python中字符串string操作

    在python中,对于字符串string的操作,我们有必要了解一下,这样在我们的以后的开发中会给我们带来很多方便 下面是我学习的笔记: #python-string #python中的字符串用单引号' ...

  6. python开发:python字符串操作方法

    name = "my \tname is {name} and i am {year} old" capitalize:第一个单词的首字母大写的方法 print(name.capi ...

  7. Python开发【第五篇】字符串

    字符串 作用:用来记录文字信息 例子: 空字符串 '' #单引号空字符串 "" #双引号空字符串 ''' ''' #三单引号空字符串 """ &quo ...

  8. Python 开发轻量级爬虫07

    Python 开发轻量级爬虫 (imooc总结07--网页解析器BeautifulSoup) BeautifulSoup下载和安装 使用pip install 安装:在命令行cmd之后输入,pip i ...

  9. Python 开发轻量级爬虫06

    Python 开发轻量级爬虫 (imooc总结06--网页解析器) 介绍网页解析器 将互联网的网页获取到本地以后,我们需要对它们进行解析才能够提取出我们需要的内容. 也就是说网页解析器是从网页中提取有 ...

随机推荐

  1. 离散化&&逆序数对

    题目:http://www.fjutacm.com/Problem.jsp?pid=3087 #include<stdio.h> #include<string.h> #inc ...

  2. linux 查看内存和cpu占用比较多的进程

    1.可以使用一下命令查使用内存最多的10个进程        ps -aux | sort -k4nr | head -n 102. 可以使用一下命令查使用CPU最多的10个进程        ps ...

  3. 72.xilinx vivado zynq vdma仿真及应用详解(一)

    很多人用zynq平台做视频图像开发,但是对vdma了解比较少,上手起来稍微有些困难,我针对这一现象,做了一个基于vivado和modelsim的仿真和应用测试工程,并写篇文章做些介绍,希望能对大家有帮 ...

  4. xv6/sh.c

    // Shell. #include "types.h" #include "user.h" #include "fcntl.h" // P ...

  5. Serv-U设置允许用户更改密码【转】

    最近,公司上了一套Serv-U10.5.0.6的ftp软件,应该是目前最新的版本了.上的第一天就遇到了一个问题,有领导发话了,他需要自己更改密码.找了N久才找到,分享一下. 点击管理界面的用户. 进入 ...

  6. python之jsonpath的使用

    import json import jsonpath import requests url="https://www.lagou.com/lbs/getAllCitySearchLabe ...

  7. IE6-IE9不支持table.innerHTML的解决方法--终极解决办法

    一.把要转译的内容放到其他属性中,例如“tt” <p class="feedback_answer_content" tt='${feedInfo.feedback_answ ...

  8. centos7-sar工具的安装过程及其简单应用

    一.sar工具安装 1.进入yum配置文件目录: cd /etc/yum.repos.d/ 2.vi CentOS-Base.repo命令创建文件CentOS-Base.repo 文件内容见网页:ht ...

  9. 关于Hazard Pointers的话题

    关于Hazard Pointers的话题, 起源于这个文章: 实现无锁的栈与队列(4) http://www.cnblogs.com/catch/p/3176636.html 其实他的系列文章(3)之 ...

  10. 【前端笔记】浅谈js继承

    我们先想想我们用js最后要怎样实现面向对象的编程.事实上我们必须用上原型链这种东西. 我们的父类superType有属性和方法,并且一些能被子类subType继承,一些能被覆盖,但是丝毫不会影响到父类 ...