加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

Python unicode在OSX上的2.6.1中工作,但在Ubuntu上的2.6.5中不工

发布时间:2020-12-20 11:16:52 所属栏目:Python 来源:网络整理
导读:鉴于从 Python解释器运行以下代码: import syssys.getdefaultencoding()my_string = 'xc3xa9'my_string = unicode(my_string,'utf-8')my_stringprint my_string 在Python上运行Python 2.6.1,一切正常: $pythonPython 2.6.1 (r261:67515,Jun 24 2010,21:4
鉴于从 Python解释器运行以下代码:

import sys
sys.getdefaultencoding()
my_string = 'xc3xa9'
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 = 'xc3xa9'
>>> 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",'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 "xc3xa9".decode("utf-8")'
Traceback (most recent call last):
  File "",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 "xc3xa9".decode("utf-8")'
é

要么

$python -c'import locale; print "xc3xa9".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().

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读