如标题,有没有理由不使用 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对象
最佳答案
当您编写 str(u'a')
时,它会使用 默认编码 将 Unicode 字符串转换为字节字符串(除非您遇到了麻烦changing it ) 将是 ASCII。
第二个版本将字符串显式编码为 UTF-8。
如果您尝试使用包含非 ASCII 字符的字符串,则差异会更加明显。第二个版本仍然有效:
>>> u'€'.encode('utf-8') '\xc2\x80'
第一个版本会给出一个异常:
>>> str(u'€') Traceback (most recent call last): File "", line 1, in str(u'€') UnicodeEncodeError: 'ascii' codec can't encode character u'\x80' in position 0: ordinal not in range(128)
关于Python str(u'a') 和 u'a'.encode ('utf-8' 之间有什么区别),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12149567/