difflib(2.1以é)ã®HtmlDiff(2.4以é)ã使ãã¨ã2ã¤ã®ããã¹ãã®å·®åãåå¾ãã¦ãHTMLã§ç°¡åã«åºåã§ãããè¡åä½ã ãã§ãªããæååä½ã®å·®åãæ示ãã¦ãããã®ã§ä¾¿å©ã
withã¹ãã¼ãã¡ã³ã(2.5以é)ã使ãã¨ããããªæãã
from __future__ import with_statement from difflib import HtmlDiff df = HtmlDiff() with open('filename1') as f: f1 = f.readlines() with open('filename2') as f: f2 = f.readlines() with open("diff.html", "w") as f: f.writelines(df.make_file(f1, f2))
ãã¾ãã«ç°¡åããã¦æ»ã¬ãã¨æã£ãã
(追è¨)æ¥æ¬èªå¯¾å¿ã«ã¤ãã¦
ããããããªããã©ãæ¥æ¬èªã¯ãã¾ãå¾æãããªãã£ã½ãã次ã®ããã«ããã
from __future__ import with_statement from difflib import HtmlDiff from codecs import open df = HtmlDiff() f1 = open('filename1', 'r', 'sjis').readlines() f2 = open('filename2', 'r', 'sjis').readlines() f = open("diff.html", "w", 'utf-8') f.writelines(df.make_file(f1, f2)) f.close()
ããã¨ããããªãµãã«æãããã
Traceback (most recent call last): File "<stdin>", line 1, in <module> File "c:\home\bin\Python25\lib\difflib.py", line 1693, in make_file context=context,numlines=numlines)) File "c:\home\bin\Python25\lib\difflib.py", line 1960, in make_table next_href[i],tolist[i])) UnicodeEncodeError: 'ascii' codec can't encode character u'\u25a0' in position 165: ordinal not in range(128)
ããã¯ãdifflibãcStringIOã¢ã¸ã¥ã¼ã«ã使ã£ã¦ããã®ãåå ãcStringIOã¢ã¸ã¥ã¼ã«ã¯(StringIOã¨éã£ã¦)Unicodeを受け付けないã®ã ã
ã¨ããããåããªããdifflib.pyã®è©²å½ç®æããããã¨ãããã§ããããªãã¨ã¯ããªãã»ããããã¨æããã©ããã対å¿ãã¾ã£ã¨ãã§ããããã
difflib.py:1948 import cStringIO s = cStringIO.StringIO() â import StringIO s = StringIO.StringIO()