我需要使用python突出显示两个简单字符串之间的差异,将不同的子字符串包含在HTML span属性中.所以我正在寻找一种简单的方法来实现以下示例所示的功能:
hightlight_diff('Hello world','HeXXo world','red')
...它应该返回字符串:
'He<span style="color:red">XX</span>o world'
我用google搜索并看到了difflib,但它应该已经过时了,我还没有找到任何好的简单演示.
我创建了一个小程序,用于检查作者数据库中是否存在作者。我尚未找到解决此问题的任何特定模块,因此我正在使用用于近似字符串匹配的模块从头开始编写它。
该数据库包含大约6000名作者,并且格式很差(许多错字,变化形式和标题,例如“ Dr.”等)。查询作者列表通常在500-1000之间(并且我有很多这样的列表),因此速度非常重要。
我的一般策略是尽可能地修剪和过滤数据库,并寻找完全匹配的内容。如果找不到匹配项,我继续进行近似字符串匹配。
我目前正在使用内置的功能,difflib.get_close_matches它完全可以实现我想要的功能,但是它非常慢(几分钟)。因此,我正在寻找其他选择:
我发现的唯一一个模糊模糊,比difflib还要慢。
我想比较两个字符串列表之间的差异。就我而言,空白是噪音,不需要显示这些差异。阅读difflib 的文档,“默认 [for charjunk] 是模块级函数IS_CHARACTER_JUNK(),它过滤掉空白字符”。完美,除了我认为它不起作用,或者有很大的不同(<-双关语!)。
import difflib
A = ['3 4\n']
B = ['3 4\n']
print ''.join(difflib.ndiff(A, B)) # default: charjunk=difflib.IS_CHARACTER_JUNK
Run Code Online (Sandbox Code Playgroud)
输出:
- 3 4
? -
+ 3 4
Run Code Online (Sandbox Code Playgroud)
我尝试了一些其他linejunk选项,但没有一个选项实际上忽略了由于空格而导致的差异。我对是什么有错误的解释charjunk吗?
作为旁注,我可以通过预处理我的字符串以使用re.sub(r'\W+', ' ', 'foo\t bar').
有没有一种优雅的方法来忽略python中的diff中的空格(使用difflib或任何其他模块)?也许我错过了一些东西,但我已经搜索了文档,并且无法在difflib中找到任何明确的支持.
我目前的解决方案是将我的文本分成单词列表,然后区分它们:
d.compare(("".join(text1_lines)).split(), ("".join(text2_lines)).split())
Run Code Online (Sandbox Code Playgroud)
这样做的缺点是,如果想要逐行差异而不是逐字逐句地报告,则必须将diff的输出与原始文件文本合并.这很容易实现,但有点不方便.
有没有办法让difflib考虑删除字符串匹配?
我已经尝试了difflib.get_close_matches()但是它不考虑在关闭匹配输出中具有较低长度的字符串.例如
from difflib import get_close_matches as gcm
x = """Erfreulich
Erfreuliche
Erfreulicher
Erfreulicherem
Erfreulicheres
Erfreulicherweis
Erfreulicherweise
Erfreuliches
Erfreulichste"""
x = [i for i in x.split("\n")]
for i in x:
print i, gcm(i,x)
Run Code Online (Sandbox Code Playgroud)
输出:
Erfreulich ['Erfreulich', 'Erfreuliche', 'Erfreuliches']
Erfreuliche ['Erfreuliche', 'Erfreuliches', 'Erfreulicher']
Erfreulicher ['Erfreulicher', 'Erfreuliche', 'Erfreulicheres']
Erfreulicherem ['Erfreulicherem', 'Erfreulicheres', 'Erfreulicher']
Erfreulicheres ['Erfreulicheres', 'Erfreulicherweis', 'Erfreulicherem']
Erfreulicherweis ['Erfreulicherweis', 'Erfreulicherweise', 'Erfreulicheres']
Erfreulicherweise ['Erfreulicherweise', 'Erfreulicherweis', 'Erfreulicheres']
Erfreuliches ['Erfreuliches', 'Erfreuliche', 'Erfreulicheres']
Erfreulichste ['Erfreulichste', 'Erfreuliche', 'Erfreuliches']
Run Code Online (Sandbox Code Playgroud)
请注意,对于字符串Erfreulicher,Erfreulich虽然距离仅为-1,但不会被视为近似匹配.
我正在 Windows 7 中使用 python 3.4。尝试比较两个文本文件,我想使用 difflib 报告它们之间的差异。以下是使用的代码:
import difflib
from difflib_data import *
with open("s1.txt") as f, open("s2.txt") as g:
flines = f.readlines()
glines = g.readlines()
d = difflib.Differ()
diff = d.compare(flines, glines)
print("\n".join(diff))
Run Code Online (Sandbox Code Playgroud)
回溯:来自 difflib_data import * ImportError:没有名为“difflib_data”的模块
如何消除这个错误...谢谢
我正在使用 difflib 来识别较长序列中短字符串的所有匹配项。然而,当有多个匹配项时,difflib 似乎只返回一个:
> sm = difflib.SequenceMatcher(None, a='ACT', b='ACTGACT')
> sm.get_matching_blocks()
[Match(a=0, b=0, size=3), Match(a=3, b=7, size=0)]
Run Code Online (Sandbox Code Playgroud)
我期望的输出是:
[Match(a=0, b=0, size=3), Match(a=0, b=4, size=3), Match(a=3, b=7, size=0)]
Run Code Online (Sandbox Code Playgroud)
实际上,字符串ACTGACT 在位置0 和4 处包含两个ACT 匹配项,大小均为3(加上字符串末尾的另一个大小为0 的匹配项)。
如何获得多个匹配项?我期待 difflib 返回两个位置。
假设我想将文件 a 和文件 b 与该difflib.diff_bytes函数进行比较,我该怎么做?
谢谢
我试图将一组字符串与一组已定义的字符串进行比较。例如,您要查找一封信件的收件人,该信件的文本是通过 OCR 数字化的。
有一个地址数组,其中包含字典作为元素。每个元素都是唯一的,包含 ID、名称、街道、邮政编码和城市。此列表将有 1000 个条目。
由于 OCR 扫描的文本可能不准确,我们需要找到与包含地址的列表最匹配的字符串候选者。
文本长度为 750 字。我们通过使用适当的过滤器函数来减少单词的数量,该函数首先按空格分割,从每个元素中剥离更多的空格,删除所有长度小于 5 个字符的单词并删除重复项;结果列表有 200 字长。
由于每个收件人有 4 个字符串(姓名街道、邮政编码和城市),其余字母长度为 200 个单词,因此我的比较必须运行 4 * 1000 * 200 = 800'000 次。
我使用 python 取得了中等成功。已正确找到匹配项。但是,该算法需要很长时间来处理大量字母(每 1500 个字母最多 50 小时)。列表理解已被应用。有没有办法正确(而不是不必要的)实现多线程?如果此应用程序需要在低规格服务器上运行怎么办?我的 6 核 CPU 没有抱怨这些任务,但是,我不知道在一个小的 AWS 实例上处理大量文档需要多少时间。
>> len(addressees)
1000
>> addressees[0]
{"Name": "John Doe", "Zip": 12345, "Street": "Boulevard of broken dreams 2", "City": "Stockholm"}
>> letter[:5] # already filtered
["Insurance", "Taxation", "Identification", "1592212", "St0ckhlm", "Mozart"]
>> from difflib import SequenceMatcher
>> def get_similarity_per_element(addressees, …Run Code Online (Sandbox Code Playgroud) 我使用 difflib 并尝试比较两个句子并找出差异。
有点像这样。
我有这段代码,但它不是逐字分析,而是逐字分析。
import difflib
# define original text
# taken from: https://en.wikipedia.org/wiki/Internet_Information_Services
original = ["IIS 8.5 has several improvements related"]
# define modified text
edited = ["It has several improvements related"]
# initiate the Differ object
d = difflib.Differ()
# calculate the difference between the two texts
diff = d.compare(original, edited)
# output the result
print ('\n'.join(diff))
Run Code Online (Sandbox Code Playgroud) difflib ×10
python ×10
diff ×2
python-3.x ×2
whitespace ×2
algorithm ×1
analysis ×1
cpu-word ×1
difference ×1
file ×1
html ×1
python-2.7 ×1
regex ×1
string ×1