python:compare
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
python:compare [2014/07/22 07:12] – [Match string with multilines] admin | python:compare [2022/10/29 16:15] (current) – external edit 127.0.0.1 | ||
---|---|---|---|
Line 92: | Line 92: | ||
a[9] and b[9] match for 0 elements | a[9] and b[9] match for 0 elements | ||
</ | </ | ||
+ | === get_opcodes === | ||
+ | <code python> | ||
+ | import difflib | ||
+ | import sys | ||
+ | |||
+ | a = """ | ||
+ | abc pq | ||
+ | ef abc | ||
+ | mn | ||
+ | """ | ||
+ | b = """ | ||
+ | ef | ||
+ | mn | ||
+ | """ | ||
+ | print 'a = ', a | ||
+ | print 'b = ', b | ||
+ | seq = difflib.SequenceMatcher(None, | ||
+ | print ' | ||
+ | for tag, alo, ahi, blo, bhi in seq.get_opcodes(): | ||
+ | print '- ', tag, alo, ahi, blo, bhi, ':' | ||
+ | print ' | ||
+ | for i in range(alo, ahi): | ||
+ | sys.stdout.writelines(a[i]) | ||
+ | print ' | ||
+ | for i in range(blo, bhi): | ||
+ | sys.stdout.writelines(b[i]) | ||
+ | result = list(difflib.ndiff(a, | ||
+ | print ' | ||
+ | print ' | ||
+ | sys.stdout.writelines(result) | ||
+ | </ | ||
+ | a = [' abcd\n', | ||
+ | b = ['abcd abcd\n', | ||
+ | ******************************* | ||
+ | - replace 0 3 0 2 : | ||
+ | --from: | ||
+ | abcd | ||
+ | abc pq | ||
+ | ef abc | ||
+ | --to: | ||
+ | abcd abcd | ||
+ | ef | ||
+ | - equal 3 4 2 3 : | ||
+ | --from: | ||
+ | mn | ||
+ | --to: | ||
+ | mn | ||
+ | ******************************* | ||
+ | normal diff: | ||
+ | - abcd | ||
+ | + abcd abcd | ||
+ | ? ++++ | ||
+ | + ef | ||
+ | - abc pq | ||
+ | - ef abc | ||
+ | mn | ||
+ | </ | ||
=== Match string with multilines === | === Match string with multilines === | ||
<code python> | <code python> | ||
Line 314: | Line 371: | ||
* etree for parsing HTML | * etree for parsing HTML | ||
Examples for lxml.html.diff: | Examples for lxml.html.diff: | ||
- | * Simple diff HTML:< | + | * Simple diff:<code python> |
+ | from os import path | ||
+ | import sys, re | ||
+ | from lxml.html import diff, etree, HTMLParser | ||
+ | import codecs | ||
+ | import StringIO | ||
+ | doc1 = '''< | ||
+ | <div class=" | ||
+ | <a title=" | ||
+ | < | ||
+ | </ | ||
+ | </ | ||
+ | <div class=" | ||
+ | <a class=" | ||
+ | <i class=" | ||
+ | < | ||
+ | </ | ||
+ | < | ||
+ | </ | ||
+ | </ | ||
+ | doc2 = '''< | ||
+ | <div class=" | ||
+ | <a title=" | ||
+ | < | ||
+ | </ | ||
+ | </ | ||
+ | <div class=" | ||
+ | <a class=" | ||
+ | <i class=" | ||
+ | < | ||
+ | </ | ||
+ | </ | ||
+ | </ | ||
+ | diffcontent = diff.htmldiff(doc1, | ||
+ | diffcontent = codecs.encode(diffcontent, | ||
+ | print diffcontent | ||
+ | </ | ||
+ | <div class=" | ||
+ | </ | ||
+ | * diff 2 HTML files:<code python> | ||
from os import path | from os import path | ||
import sys, re | import sys, re |
python/compare.1406013133.txt.gz · Last modified: 2022/10/29 16:15 (external edit)