python中differ_具有上下文差异的Python difflib.Differ(Python difflib.Differ with Contextual difference)...

具有上下文差异的Python difflib.Differ(Python difflib.Differ with Contextual difference)

如何使用difflib.Differ()比较一行中的字符,从而获得上下文差异(仅限具有差异的行而不是所有行)

>>> text1 = ''' 1. 111

... 2. 222

... 3. 333

... 4. 444

... '''.splitlines(1)

>>> text2 = ''' 1. 121 xxx

... 2. 222

... 3. 313

... 4. 444

... '''.splitlines(1)

>>> from difflib import Differ

>>> d = Differ()

>>>

>>> print ''.join(d.compare(text1, text2))

- 1. 111

+ 1. 121 xxx

2. 222

- 3. 333

? ^

+ 3. 313

? ^

4. 444

>>>

# I want something like this with context=True

>>> print ''.join(d.compare(text1, text2))

- 1. 111

+ 1. 121 xxx

- 3. 333

? ^

+ 3. 313

? ^

How can I get contextual difference (e.i only the lines with difference and not all lines) along with comparing the characters within a Line using difflib.Differ()

Example

>>> text1 = ''' 1. 111

... 2. 222

... 3. 333

... 4. 444

... '''.splitlines(1)

>>> text2 = ''' 1. 121 xxx

... 2. 222

... 3. 313

... 4. 444

... '''.splitlines(1)

>>> from difflib import Differ

>>> d = Differ()

>>>

>>> print ''.join(d.compare(text1, text2))

- 1. 111

+ 1. 121 xxx

2. 222

- 3. 333

? ^

+ 3. 313

? ^

4. 444

>>>

# I want something like this with context=True

>>> print ''.join(d.compare(text1, text2))

- 1. 111

+ 1. 121 xxx

- 3. 333

? ^

+ 3. 313

? ^

原文:https://stackoverflow.com/questions/24689976

2020-04-01 08:04

满意答案

显然,您可以过滤结果,删除以空格开头的行。 列表理解和str.startswith可以做到这一点。

>>> from difflib import Differ

>>> d = Differ()

>>> print ''.join(line for line in d.compare(text1, text2) if not line.startswith(' '))

- 1. 111

+ 1. 121 xxx

- 3. 333

? ^

+ 3. 313

? ^

Obviously you can filter the results, removing lines that start with whitespace. A list comprehension and str.startswith can do that.

>>> from difflib import Differ

>>> d = Differ()

>>> print ''.join(line for line in d.compare(text1, text2) if not line.startswith(' '))

- 1. 111

+ 1. 121 xxx

- 3. 333

? ^

+ 3. 313

? ^

2014-07-11

相关问答

Differ.compare()的参数应该是字符串序列。 如果你使用两个字符串,它们将被视为序列,因此逐个字符进行比较。 因此,您的示例应该重写为: url = 'https://secure.ssa.gov/apps10/reference.nsf/instructiontypecode!openview&restricttocategory=POMT'

response = urllib.urlopen(url)

content = response.readlines() # get re...

首先,确实,Differ()。compare()比较行而不是句子。 其次,它实际上比较了序列,例如字符串列表。 但是,您传递两个字符串,而不是两个字符串列表。 由于字符串也是一个序列(字符),因此在您的情况下,Differ()。compare()会比较各个字符。 如果要按句子比较文件,则必须准备两个句子列表。 您可以使用nltk.sent_tokenize(text)将字符串拆分为句子。 diff = differ.compare(nltk.sent_tokenize(text),nltk.sen...

我想我已经完成了我想要的PHP Diff输出。 def sdiffer(s1, s2):

differ = difflib.Differ()

diffs = list(differ.compare(s1, s2))

i = 0

sdiffs = []

length = len(diffs)

sequence = 0

while i < length:

line = diffs[i][2:]

if diffs[...

区别必须包含足够的信息才能将版本修补到另一个版本,所以是的,对于单行更改为非常小的文档的实验,存储整个文档可能会更便宜。 库函数返回迭代器,以便在内存紧张的客户端上更简单,或者只需查看部分结果序列。 在Python中没问题,因为每个迭代器都可以转换成一个list(an_iterator)表达式。 大多数差异是在文本行上完成的,但是可以difflib ,而difflib执行。 查看difflib中的Differ类对象。 遍布各地的例子使用人性化的输出,但差异是以一种更加紧凑,计算机友好的方式在内部管...

Difflib尊重参数的排序。 它基本上显示了将一个序列转换为另一个序列的编辑。 如果您不关心订单,可能会出现您想要的设定差异: >>> {1, 2, 3} - {0, 2, 1}

set([3])

>>> {0, 2, 1} - {1, 2, 3}

set([0])

Difflib respects the ordering of arguments. It essentially shows the edits that would transform one sequence into a...

您的示例的主要问题是您如何处理结束字符。 如果您在输入中完全替换它们,输出将不再正确排列,因此没有任何意义。 要解决这个问题, readable_whitespace函数应该如下所示: def readable_whitespace(line):

end = len(line.rstrip('\r\n'))

return line[:end] + repr(line[end:])[1:-1] + '\n'

这将处理所有类型的结束序列,并确保在打印时正确显示行。 另一个小问题是由于...

经过一番研究,我建议使用amatch或SimMetrics (使用JRuby)并手动实现get_close_matches方法。 两个库都提供了许多字符串相似度算法的实现。 After some research, I suggest using amatch or SimMetrics (with JRuby) and manually implement the get_close_matches method. Both libs offer implementations of many ...

Charles Duffy的评论非常有用。 但是,由于这些行的输出是空白的,因为它们试图读取文件内容两次: line_num1=sum(1 for line in xmlfile1)

line_num2=sum(1 for line in xmlfile2)

diffList = list(diffInstance.compare(xmlfile1.readlines(), xmlfile2.readlines()))

要计算line_num1您正在读取xmlfile1每一行,这意味着存储在x...

这可能有用,它至少通过你的演示测试:编辑:我已经做了一些修改来处理一些字符串索引问题。 我相信它现在应该是好的。 #include

#include

#include

#include

#include

bool starts_with(const std::string &s1, const std::string &s2) {

return (s1.length() <= s2.len...

显然,您可以过滤结果,删除以空格开头的行。 列表理解和str.startswith可以做到这一点。 >>> from difflib import Differ

>>> d = Differ()

>>> print ''.join(line for line in d.compare(text1, text2) if not line.startswith(' '))

- 1. 111

+ 1. 121 xxx

- 3. 333

? ^

+ 3. 313

? ...

相关文章

Python 编程语言具有很高的灵活性,它支持多种编程方法,包括过程化的、面向对象的和函数式的。但最重

...

该程序是在python2.3上完成的,python版本间有差异。 Mapper: import sys

...

python2和python3的区别,1.性能 Py3.0运行 pystone benchmark的速

...

Python的文件类型 Python有三种文件类型,分别是源代码文件、字节码文件和优化代码文件

源代

...

python的官网:http://www.python.org/ 有两个版本,就像struts1和st

...

从这篇文章中我们已经了解了微信公众平台消息传递的方式,这种方式有一个先天的缺陷:不同用户的请求都来自同

...

好久没有写了,还不是近期刚过的期末考试和期中考试 最近因为一个微信公众平台大赛在学phthon 找了本

...

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值