[Tutor] difflib context to string-object?

Steven D'Aprano steve at pearwood.info
Sat Mar 20 01:25:07 CET 2010


On Fri, 19 Mar 2010 09:55:06 pm Karjer Jdfjdf wrote:
> With difflib.context_diff it is possible to write the context to
> files.
>
> difflib.context_diff(a, b[, fromfile][, tofile][, fromfiledate][,
> tofiledate][, n][, lineterm])
>
> Is it also possible to do this to seperate string-objects instead of
> writing them to files?


Use StringIO objects, which are fake files that can be read as strings.

Untested and entirely from memory, use:

from StringIO import StringIO
fromfile = StringIO("some text goes here")
tofile = StringIO("some different text goes here")




-- 
Steven D'Aprano


More information about the Tutor mailing list