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

Qiangning Hong hongqn at gmail.com
Fri Dec 26 06:44:11 CET 2008


>>> u = u'\u554a'
>>> print u
啊
>>> 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'

Quote from file object's documentation:

encoding
The encoding that this file uses. When Unicode strings are written to
a file, they will be converted to byte strings using this
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?



More information about the Python-list mailing list