python中字符编码叙述正确的是_Unicode问题,在python中正确解码/编码字符串

我不知道你收到这条消息的方式和地点,但看看这个例子:

$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.

>>> txt = u'Dassault Myst\xe8re'

>>> txt

u'Dassault Myst\xe8re'

>>> print txt

Traceback (most recent call last):

File "", line 1, in

UnicodeEncodeError: 'ascii' codec can't encode character u'\xe8' in position 13:

ordinal not in range(128)

>>> ^D

$export LANG=en_US.UTF-8

$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.

>>> txt = u'Dassault Myst\xe8re'

>>> txt

u'Dassault Myst\xe8re'

>>> print txt

Dassault Mystère

>>>^D

因此,您可以看到是否有一个控制台作为ASCII,然后在打印期间,有一个从unicode到ascii的转换,如果在ASCII范围外有字符,则抛出异常.

但是如果控制台可以接受unicode,那么一切都会正确显示.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值