GVKun编程网logo

Python difflib:内联突出显示差异?(python内联函数)

9

在这篇文章中,我们将带领您了解Pythondifflib:内联突出显示差异?的全貌,包括python内联函数的相关情况。同时,我们还将为您介绍有关ipynb:在VSCodeipythonnoteboo

在这篇文章中,我们将带领您了解Python difflib:内联突出显示差异?的全貌,包括python内联函数的相关情况。同时,我们还将为您介绍有关ipynb:在 VSCode ipython notebooks 中突出显示所选变量的其他出现、php – 如何突出显示另一个文本段落中的更改/差异?、python difflib模块示例讲解、python difflib模块详解的知识,以帮助您更好地理解这个主题。

本文目录一览:

Python difflib:内联突出显示差异?(python内联函数)

Python difflib:内联突出显示差异?(python内联函数)

比较相似的行时,我要强调同一行的区别:

a) lorem ipsum dolor sit ametb) lorem foo ipsum dolor ametlorem <ins>foo</ins> ipsum dolor <del>sit</del> amet

尽管difflib.HtmlDiff似乎进行了这种内联突出显示,但它会产生非常冗长的标记。

不幸的是,我无法找到另一种不能逐行运行的类/方法。

我有什么想念的吗?任何指针将不胜感激!

答案1

小编典典

对于您的简单示例:

import difflibdef show_diff(seqm):    """Unify operations between two compared stringsseqm is a difflib.SequenceMatcher instance whose a & b are strings"""    output= []    for opcode, a0, a1, b0, b1 in seqm.get_opcodes():        if opcode == ''equal'':            output.append(seqm.a[a0:a1])        elif opcode == ''insert'':            output.append("<ins>" + seqm.b[b0:b1] + "</ins>")        elif opcode == ''delete'':            output.append("<del>" + seqm.a[a0:a1] + "</del>")        elif opcode == ''replace'':            raise NotImplementedError, "what to do with ''replace'' opcode?"        else:            raise RuntimeError, "unexpected opcode"    return ''''.join(output)>>> sm= difflib.SequenceMatcher(None, "lorem ipsum dolor sit amet", "lorem foo ipsum dolor amet")>>> show_diff(sm)''lorem<ins> foo</ins> ipsum dolor <del>sit </del>amet''

这适用于字符串。您应该决定如何处理“替换”操作码。

ipynb:在 VSCode ipython notebooks 中突出显示所选变量的其他出现

ipynb:在 VSCode ipython notebooks 中突出显示所选变量的其他出现

如何解决ipynb:在 VSCode ipython notebooks 中突出显示所选变量的其他出现?

在其他语言中,VSCode 会突出显示所选变量的出现。

在下面的 Elixir language 代码示例中,我的光标位于第二行的变量 SELECT parent.entity_id AS parent_id,simple.entity_id AS simple_id,simple.sku AS simple_sku,cpp.rule_price AS rule_price,MAX(IF(cped.attribute_code = ''price'',cped.value,NULL)) AS price,MAX(IF(cped.attribute_code = ''special_price'',NULL)) AS special_price,cpev.value AS simple_name,parent.type_id AS type_id,wi.store_id AS wishlist_store_id FROM catalog_product_entity AS parent LEFT JOIN catalog_product_super_link AS link ON parent.row_id = link.parent_id LEFT JOIN catalog_product_entity AS simple ON link.product_id = simple.entity_id LEFT JOIN catalogrule_product_price AS cpp ON cpp.product_id = simple.entity_id AND cpp.website_id = (SELECT `website_id` FROM `store` WHERE `store_id` = ''13'') LEFT JOIN (SELECT cped.row_id,ea.attribute_code FROM catalog_product_entity_decimal AS cped JOIN eav_attribute AS ea ON ea.attribute_id = cped.attribute_id WHERE ea.attribute_code IN (''price'',''special_price'') AND cped.store_id IN (13,0)) AS cped ON cped.row_id = simple.entity_id LEFT JOIN catalog_product_entity_varchar AS cpev ON cpev.row_id = simple.entity_id AND cpev.store_id IN (13,0) AND cpev.attribute_id = ( SELECT `attribute_id` FROM `eav_attribute` WHERE `attribute_code` IN (''name'') order by `attribute_id` DESC LIMIT 1) LEFT JOIN wishlist_item AS wi ON parent.row_id = wi.product_id AND `wishlist_id` = '''' WHERE parent.entity_id = ''157161'' GROUP BY parent_id,simple_id,simple_sku,rule_price,simple_name,type_id,wishlist_store_id LIMIT 1 上,但另一个事件(第一行)也突出显示:

var pid highlighted

如何使用 pid 文件实现此目的?

解决方法

抱歉,您是这个意思吗?

enter image description here

enter image description here

我的 Jupyter 和 python 文件总是这样工作。

php – 如何突出显示另一个文本段落中的更改/差异?

php – 如何突出显示另一个文本段落中的更改/差异?

是否可以突出显示另一个文本段落中的修改?

例如,数据库中有3个文本字段.非管理员用户可以编辑文本并提交审批.当管理员登录时,他可以打开批准页面,并显示原始文本和用户提交的文本以及修改.通常这些修改本质上非常小,例如拼写纠正或仅删除或添加句子.

是否可以通过突出显示修改(来自原始文本)的方式呈现,以便管理员用户在批准之前不必检查每个字段和每一行?

任何帮助,将不胜感激.
谢谢.

更新:
有点类似于TortoiseSVN的代码更改突出显示.

解决方法:

选项:

> PHP Inline Diff(使用PEAR text-diff)
> PEAR Text_Diff
> Diff in Pure PHP
> xdiff_string_diff()

Stackoverflow存档:

> Highlight the difference between two strings in PHP
> Apply Diff in PHP
> Need Help Optimizing php string difference function

python difflib模块示例讲解

python difflib模块示例讲解

difflib模块提供的类和方法用来进行序列的差异化比较,它能够比对文件并生成差异结果文本或者html格式的差异化比较页面,如果需要比较目录的不同,可以使用filecmp模块。

class difflib.SequenceMatcher

此类提供了比较任意可哈希类型序列对方法。此方法将寻找没有包含‘垃圾'元素的最大连续匹配序列。

通过对算法的复杂度比较,它由于原始的完形匹配算法,在最坏情况下有n的平方次运算,在最好情况下,具有线性的效率。

它具有自动垃圾启发式,可以将重复超过片段1%或者重复200次的字符作为垃圾来处理。可以通过将autojunk设置为false关闭该功能。

class difflib.Differ

此类比较的是文本行的差异并且产生适合人类阅读的差异结果或者增量结果,结果中各部分的表示如下:

这里写图片描述

class difflib.HtmlDiff

 此类可以被用来创建HTML表格 (或者说包含表格的html文件) ,两边对应展示或者行对行的展示比对差异结果。

 make_file(fromlines,tolines [,fromdesc][,todesc][,context][,numlines])

make_table(fromlines,numlines])

以上两个方法都可以用来生成包含一个内容为比对结果的表格的html文件,并且部分内容会高亮显示。

difflib.context_diff(a,b[,fromfile][,tofile][,fromfiledate][,tofiledate][,n][,lineterm])

比较a与b(字符串列表),并且返回一个差异文本行的生成器
示例:

>>> s1 = ['bacon\n','eggs\n','ham\n','guido\n']
>>> s2 = ['python\n','eggy\n','hamster\n','guido\n']
>>> for line in context_diff(s1,s2,fromfile='before.py',tofile='after.py'):
...   sys.stdout.write(line) 
*** before.py
--- after.py
***************
*** 1,4 ****
! bacon
! eggs
! ham
 guido
--- 1,4 ----
! python
! eggy
! hamster
 guido

difflib.get_close_matches(word,possibilities[,cutoff])

返回最大匹配结果的列表

示例:

>>> get_close_matches('appel',['ape','apple','peach','puppy'])
['apple','ape']
>>> import keyword
>>> get_close_matches('wheel',keyword.kwlist)
['while']
>>> get_close_matches('apple',keyword.kwlist)
[]
>>> get_close_matches('accept',keyword.kwlist)
['except']

difflib.ndiff(a,linejunk][,charjunk])

比较a与b(字符串列表),返回一个Differ-style 的差异结果
示例:

>>> diff = ndiff('one\ntwo\nthree\n'.splitlines(1),...       'ore\ntree\nemu\n'.splitlines(1))
>>> print ''.join(diff),- one
? ^
+ ore
? ^
- two
- three
? -
+ tree
+ emu

difflib.restore(sequence,which)

返回一个由两个比对序列产生的结果

示例

>>> diff = ndiff('one\ntwo\nthree\n'.splitlines(1),...       'ore\ntree\nemu\n'.splitlines(1))
>>> diff = list(diff) # materialize the generated delta into a list
>>> print ''.join(restore(diff,1)),one
two
three
>>> print ''.join(restore(diff,2)),ore
tree
emu

difflib.unified_diff(a,lineterm])

比较a与b(字符串列表),返回一个unified diff格式的差异结果.

示例:

>>> s1 = ['bacon\n','guido\n']
>>> for line in unified_diff(s1,tofile='after.py'):
...  sys.stdout.write(line) 
--- before.py
+++ after.py
@@ -1,4 +1,4 @@
-bacon
-eggs
-ham
+python
+eggy
+hamster
 guido

实际应用示例

比对两个文件,然后生成一个展示差异结果的HTML文件

#coding:utf-8
'''
file:difflibeg.py
date:2017/9/9 10:33
author:lockey
email:lockey@123.com
desc:diffle module learning and practising 
'''
import difflib
hd = difflib.HtmlDiff()
loads = ''
with open('G:/python/note/day09/0907code/hostinfo/cpu.py','r') as load:
 loads = load.readlines()
 load.close()

mems = ''
with open('G:/python/note/day09/0907code/hostinfo/mem.py','r') as mem:
 mems = mem.readlines()
 mem.close()

with open('htmlout.html','a+') as fo:
 fo.write(hd.make_file(loads,mems))
 fo.close()

运行结果:

这里写图片描述

生成的html文件比对结果:

这里写图片描述

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。

python difflib模块详解

python difflib模块详解

这篇文章主要为大家详细介绍了python difflib模块的示例,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

difflib模块提供的类和方法用来进行序列的差异化比较,它能够比对文件并生成差异结果文本或者html格式的差异化比较页面,如果需要比较目录的不同,可以使用filecmp模块。

class difflib.SequenceMatcher

此类提供了比较任意可哈希类型序列对方法。此方法将寻找没有包含‘垃圾''元素的最大连续匹配序列。

通过对算法的复杂度比较,它由于原始的完形匹配算法,在最坏情况下有n的平方次运算,在最好情况下,具有线性的效率。

立即学习“Python免费学习笔记(深入)”;

它具有自动垃圾启发式,可以将重复超过片段1%或者重复200次的字符作为垃圾来处理。可以通过将autojunk设置为false关闭该功能。

class difflib.Differ

此类比较的是文本行的差异并且产生适合人类阅读的差异结果或者增量结果,结果中各部分的表示如下:

这里写图片描述

class difflib.HtmlDiff

 此类可以被用来创建HTML表格 (或者说包含表格的html文件) ,两边对应展示或者行对行的展示比对差异结果。

 make_file(fromlines, tolines [, fromdesc][, todesc][, context][, numlines])

make_table(fromlines, tolines [, fromdesc][, todesc][, context][, numlines])

以上两个方法都可以用来生成包含一个内容为比对结果的表格的html文件,并且部分内容会高亮显示。

difflib.context_diff(a, b[, fromfile][, tofile][, fromfiledate][, tofiledate][, n][, lineterm])

比较a与b(字符串列表),并且返回一个差异文本行的生成器
示例:


>>> s1 = [&#39;bacon\n&#39;, &#39;eggs\n&#39;, &#39;ham\n&#39;, &#39;guido\n&#39;]
>>> s2 = [&#39;python\n&#39;, &#39;eggy\n&#39;, &#39;hamster\n&#39;, &#39;guido\n&#39;]
>>> for line in context_diff(s1, s2, fromfile=&#39;before.py&#39;, tofile=&#39;after.py&#39;):
...   sys.stdout.write(line) 
*** before.py
--- after.py
***************
*** 1,4 ****
! bacon
! eggs
! ham
 guido
--- 1,4 ----
! python
! eggy
! hamster
 guido
登录后复制

difflib.get_close_matches(word, possibilities[, n][, cutoff])

返回最大匹配结果的列表

示例:


>>> get_close_matches(&#39;appel&#39;, [&#39;ape&#39;, &#39;apple&#39;, &#39;peach&#39;, &#39;puppy&#39;])
[&#39;apple&#39;, &#39;ape&#39;]
>>> import keyword
>>> get_close_matches(&#39;wheel&#39;, keyword.kwlist)
[&#39;while&#39;]
>>> get_close_matches(&#39;apple&#39;, keyword.kwlist)
[]
>>> get_close_matches(&#39;accept&#39;, keyword.kwlist)
[&#39;except&#39;]
登录后复制

difflib.ndiff(a, b[, linejunk][, charjunk])

比较a与b(字符串列表),返回一个Differ-style 的差异结果
示例:


>>> diff = ndiff(&#39;one\ntwo\nthree\n&#39;.splitlines(1),
...       &#39;ore\ntree\nemu\n&#39;.splitlines(1))
>>> print &#39;&#39;.join(diff),
- one
? ^
+ ore
? ^
- two
- three
? -
+ tree
+ emu
登录后复制

difflib.restore(sequence, which)

返回一个由两个比对序列产生的结果

示例


>>> diff = ndiff(&#39;one\ntwo\nthree\n&#39;.splitlines(1),
...       &#39;ore\ntree\nemu\n&#39;.splitlines(1))
>>> diff = list(diff) # materialize the generated delta into a list
>>> print &#39;&#39;.join(restore(diff, 1)),
one
two
three
>>> print &#39;&#39;.join(restore(diff, 2)),
ore
tree
emu
登录后复制

difflib.unified_diff(a, b[, fromfile][, tofile][, fromfiledate][, tofiledate][, n][, lineterm])

比较a与b(字符串列表),返回一个unified diff格式的差异结果.

示例:


>>> s1 = [&#39;bacon\n&#39;, &#39;eggs\n&#39;, &#39;ham\n&#39;, &#39;guido\n&#39;]
>>> s2 = [&#39;python\n&#39;, &#39;eggy\n&#39;, &#39;hamster\n&#39;, &#39;guido\n&#39;]
>>> for line in unified_diff(s1, s2, fromfile=&#39;before.py&#39;, tofile=&#39;after.py&#39;):
...  sys.stdout.write(line) 
--- before.py
+++ after.py
@@ -1,4 +1,4 @@
-bacon
-eggs
-ham
+python
+eggy
+hamster
 guido
登录后复制

实际应用示例

比对两个文件,然后生成一个展示差异结果的HTML文件


#coding:utf-8
&#39;&#39;&#39;
file:difflibeg.py
date:2017/9/9 10:33
author:lockey
email:lockey@123.com
desc:diffle module learning and practising 
&#39;&#39;&#39;
import difflib
hd = difflib.HtmlDiff()
loads = &#39;&#39;
with open(&#39;G:/python/note/day09/0907code/hostinfo/cpu.py&#39;,&#39;r&#39;) as load:
 loads = load.readlines()
 load.close()

mems = &#39;&#39;
with open(&#39;G:/python/note/day09/0907code/hostinfo/mem.py&#39;, &#39;r&#39;) as mem:
 mems = mem.readlines()
 mem.close()

with open(&#39;htmlout.html&#39;,&#39;a+&#39;) as fo:
 fo.write(hd.make_file(loads,mems))
 fo.close()
登录后复制

运行结果:

这里写图片描述

生成的html文件比对结果:

这里写图片描述

以上就是python difflib模块详解的详细内容,更多请关注php中文网其它相关文章!

今天关于Python difflib:内联突出显示差异?python内联函数的讲解已经结束,谢谢您的阅读,如果想了解更多关于ipynb:在 VSCode ipython notebooks 中突出显示所选变量的其他出现、php – 如何突出显示另一个文本段落中的更改/差异?、python difflib模块示例讲解、python difflib模块详解的相关知识,请在本站搜索。

本文标签: