En Tue, 29 May 2007 15:16:52 -0300, Ragnar Ouchterlony <[EMAIL PROTECTED]> escribió:
> If I for some reason can't open the object myself or needs encoding on > other file-like objects, I think the following wrapper function is of > use (it essentially does what codecs.open() does but takes a file-object > instead of a filename): > > def filewrapper(f, encoding=None, errors='strict'): > if encoding is None: > return f > > info = codecs.lookup(encoding) > srw = codecs.StreamReaderWriter(f, info.streamreader, > info.streamwriter, errors) > # Add attributes to simplify introspection > srw.encoding = encoding > return srw > > I find this especially useful for changing how stdout and friends does > it's encoding, e.g: > >>>> sys.stdout = filewrapper(sys.stdout, 'utf-8') >>>> print u"åäö \N{GREEK CAPITAL LETTER DELTA}" > > Useful if you don't want to append .encode() to everything you print out > that potentially can contain non-ascii letters. Isn't the same as codecs.EncodedFile? -- Gabriel Genellina -- http://mail.python.org/mailman/listinfo/python-list