以下代码检查了float()
输入非 ascii 符号时方法的行为:
import sys
try:
float(u'\xbd')
except ValueError as e:
print sys.getdefaultencoding() # in my system, this is 'ascii'
print e[0].decode('latin-1') # u'invalid literal for float(): ' followed by the 1/2 (one half) character
print unicode(e[0]) # raises "UnicodeDecodeError: 'ascii' codec can't decode byte 0xbd in position 29: ordinal not in range(128)"
我的问题:为什么错误信息e[0]
用 Latin-1 编码?默认编码是 Ascii,这似乎是unicode()
预期的。
平台是 Ubuntu 9.04,Python 2.6.2