Python str(u'a') 和 u'a'.encode ('utf-8' 之间有什么区别)

标签 python unicode

如标题,有没有理由不使用 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/

相关文章:

python - 从执行存储过程的 psycopg2 游标获取列名列表?

python - 在tornadoweb websockets服务器中实现SSL

python - 从哪里获得 RAND_egd?

Java - 如何使用阿拉伯字符?

delphi - D2009 TStringlist ansisstring

python ascii 到 unicode 转换

python - 从 lxml 获取内部 xml

python virtualenv ImportError 没有名为 inspect time flask 的模块

java - 如果我从文件中读取,如何将 ASCII(Unicode Escaped)中的字符串转换为 Unicode(UTF-8)?

pdf - 为什么在 PHP 中使用 FPDF 亚洲 unicode 字符没有出现在 PDF 上?