鉴于从
Python解释器运行以下代码:
import sys
sys.getdefaultencoding()
my_string = '\xc3\xa9'
my_string = unicode(my_string, 'utf-8')
my_string
print my_string
在Python上运行Python 2.6.1,一切正常:
$python
Python 2.6.1 (r261:67515, Jun 24 2010, 21:47:49)
[GCC 4.2.1 (Apple Inc. build 5646)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.getdefaultencoding()
'ascii'
>>> my_string = '\xc3\xa9'
>>> my_string = unicode(my_string, 'utf-8')
>>> my_string
u'\xe9'
>>> print my_string
é
>>>
在Ubuntu 10.04 LTS上运行Python 2.6.5时,它失败了:
$python
Python 2.6.5 (r265:79063, Apr 16 2010, 13:57:41)
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.getdefaultencoding()
'ascii'
>>> my_string = '\xc3\xa9'
>>> my_string = unicode(my_string, 'utf-8')
>>> my_string
u'\xe9'
>>> print my_string
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' in position 0: ordinal not in range(128)
>>>
Python 2.6.1和2.6.5之间有什么变化,需要不同的unicode字符串处理?或者这与我的(默认的Ubuntu服务器10.04 LTS)linux环境中配置错误有什么关系?
编辑:两个环境都有LANG = en_US.UTF-8
最佳答案 我可以使用以下命令重现错误:
$PYTHONIOENCODING=ascii python -c'print "\xc3\xa9".decode("utf-8")'
Traceback (most recent call last): File "", line 1, in UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' in position 0:\ ordinal not in range(128)
sys.getdefaultencoding()
是’ascii’,默认情况下不是很有用.
尝试使用您的控制台编码:
$PYTHONIOENCODING=utf-8 python -c'print "\xc3\xa9".decode("utf-8")'
é
要么
$python -c'import locale; print "\xc3\xa9".decode("utf-8").encode(
> locale.getpreferredencoding())'
é
检查sys.stdout.encoding:
$python -c'import sys; o = sys.stdout; print o.isatty(), o.encoding'
True UTF-8
$python -c'import sys; o = sys.stdout; print o.isatty(), o.encoding' | cat
False None
$python -c'import sys; o = sys.stdout; print o.isatty(), o.encoding' >/tmp/out
$cat /tmp/out
False None
如果sys.stdout.encoding为None,请尝试使用locale.getpreferredencoding()
或设置PYTHONIOENCODING
,如上所示.见http://wiki.python.org/moin/PrintFails
如果错误仅发生在交互式Python会话中,请查看sys.displayhook()
.