在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. Perl6多线程3: Promise start / in / await

    创建一个Promise 并自动运行: my $p = Promise.start({say 'Hello, Promise!'}); 如果把代码改成如下, 我们会发现什么也没打印: ;say 'Hel ...

  2. SQLite3 C/C++ 开发接口简介(API函数)

    from : http://www.sqlite.com.cn/MySqlite/5/251.Html 1.0 总览 SQLite3是SQLite一个全新的版本,它虽然是在SQLite 2.8.13的 ...

  3. 判断Selenium加载完成

    How do you make Selenium 2.0 wait for the page to load? You can also check pageloaded using followin ...

  4. 42.Trapping Rain Water---dp,stack,两指针

    题目链接:https://leetcode.com/problems/trapping-rain-water/description/ 题目大意:与84题做比较,在直方图中计算其蓄水能力.例子如下: ...

  5. 【UOJ#38】【清华集训2014】奇数国

    考虑欧拉函数的性质,60很小,压位存下线段树每个节点出现质数. #include<bits/stdc++.h> ; ; typedef long long ll; using namesp ...

  6. 服务号使用微信网页授权(H5应用等)

    获取授权准备 AppId 服务号已经认证且获取到响应接口权限 设置网页授权域名 公众号设置 - 功能设置 - 网页授权域名.注意事项: 回调页面域名或路径需使用字母.数字及"-"的 ...

  7. 百度NLP二面-电话面

    实验室项目:1.实验室方向 2.用两分钟介绍自己的项目,创新点在哪里 个人项目:     1.自己实现的贝叶斯分类器,目的,怎么做的 2.怎么计算各个分类的先验.(因为我使用的训练预料是每个分类10篇 ...

  8. 洛谷 P1469 找筷子 题解

    题目传送门 先排序一遍,再一个一个判断是否有偶数个.注意for循环要i+=2. #include<bits/stdc++.h> using namespace std; ]; int ma ...

  9. C++中对已分配空间的指针调用一个类的构造函数

    在看MINIBASE的源代码的时候发现里面有类似于这样的东西 bufTable = (BufDesc*) MINIBASE_SHMEM->malloc( numBuffers * sizeof( ...

  10. new Date()时间

    var myDate = new Date(); myDate.toLocaleDateString():可以获取当前日期myDate.toLocaleTimeString(); 可以获取当前时间 扩 ...