sys.stdout.write()'s bug or doc bug?

Martin martin at marcher.name
Fri Dec 26 11:31:47 EST 2008


Sorry GMAIL got me again, I sent this in private first, apologies.

hi,



2008/12/26 Qiangning Hong <hongqn at gmail.com>:
>>>> sys.stdout.write(u)
> Traceback (most recent call last):
>  File "<stdin>", line 1, in <module>
> UnicodeEncodeError: 'ascii' codec can't encode character u'\u554a' in
> position 0: ordinal not in range(128)
>>>> type(sys.stdout)
> <type 'file'>
>>>> sys.stdout.encoding
> 'UTF-8'

Python 2.4.4 (#2, Oct 22 2008, 19:52:44)
[GCC 4.1.2 20061115 (prerelease) (Debian 4.1.1-21)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> u = u"\u554a"
>>> print u
啊
>>> sys.stdout.write(u + "\n")
Traceback (most recent call last):
 File "<stdin>", line 1, in ?
UnicodeEncodeError: 'ascii' codec can't encode character u'\u554a' in
position 0: ordinal not in range(128)
>>> # you are trying to write unicode, you need to encode it to something that suits your needs
>>> sys.stdout.write(u.encode("UTF-8") + "\n")
啊
>>> # now go and write a hundred times "Unicode is not an encoding" :)

> So, my question is, as sys.stdout IS a file object, why it does not
> use its encoding attribute to convert the given unicode?  An
> implementation bug? A documenation bug?

hmm I always thought "sys.stdout" is a "file-like object" not that it IS a file.

/martin

--
http://soup.alt.delete.co.at
http://www.xing.com/profile/Martin_Marcher
http://www.linkedin.com/in/martinmarcher

You are not free to read this message,
by doing so, you have violated my licence
and are required to urinate publicly. Thank you.

Please avoid sending me Word or PowerPoint attachments.
See http://www.gnu.org/philosophy/no-word-attachments.html


More information about the Python-list mailing list