how to print the GREEK CAPITAL LETTER delta under utf-8 encoding
Gabriel Genellina
gagsl-py2 at yahoo.com.ar
Tue May 29 15:08:34 EDT 2007
En Tue, 29 May 2007 15:16:52 -0300, Ragnar Ouchterlony
<ragnar at lysator.liu.se> 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
More information about the Python-list
mailing list