Unicode blues in Python3
Antoine Pitrou
solipsis at pitrou.net
Wed Mar 24 09:41:11 EDT 2010
Le Tue, 23 Mar 2010 10:33:33 -0700, nn a écrit :
> I know that unicode is the way to go in Python 3.1, but it is getting in
> my way right now in my Unix scripts. How do I write a chr(253) to a
> file?
>
> #nntst2.py
> import sys,codecs
> mychar=chr(253)
> print(sys.stdout.encoding)
> print(mychar)
print() writes to the text (unicode) layer of sys.stdout.
If you want to access the binary (bytes) layer, you must use
sys.stdout.buffer. So:
sys.stdout.buffer.write(chr(253).encode('latin1'))
or:
sys.stdout.buffer.write(bytes([253]))
See http://docs.python.org/py3k/library/io.html#io.TextIOBase.buffer
More information about the Python-list
mailing list