8

作为标题,是否有理由不使用 str() 将 unicode 字符串转换为 str?

>>> str(u'a')
'a'
>>> str(u'a').__class__
<type 'str'>
>>> u'a'.encode('utf-8')
'a'
>>> u'a'.encode('utf-8').__class__
<type 'str'>
>>> u'a'.encode().__class__
<type 'str'>

更新:感谢您的回答,也不知道我是否使用特殊字符创建了一个字符串,它会自动转换为 utf-8

>>> a = '€'
>>> a.__class__
<type 'str'>
>>> a
'\xe2\x82\xac'

也是 python 3 中的 Unicode 对象

4

1 回答 1

19

当您编写它时,它将使用默认编码str(u'a')将 Unicode 字符串转换为字节字符串(除非您已经麻烦地更改它)将是 ASCII。

第二个版本将字符串显式编码为 UTF-8。

如果您尝试使用包含非 ASCII 字符的字符串,则差异会更加明显。第二个版本仍然有效:

>>> u'€'.encode('utf-8')
'\xc2\x80'

第一个版本会给出一个例外:

>>> str(u'€')

回溯(最近一次通话最后):
  文件“”,第 1 行,在
    str(u'€')
UnicodeEncodeError:“ascii”编解码器无法在位置 0 编码字符 u'\x80':序数不在范围内(128)
于 2012-08-27T21:04:50.893 回答