2

difflib.Differ() を使用して Line 内の文字を比較するとともに、コンテキストの違い (すべての行ではなく、違いのある行のみ) を取得するにはどうすればよいですか?

>>> 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
?       ^

更新: ここで答えました: python difflib character diff with unifed contextual format

4

1 に答える 1