python - python2内部如何处理字符串和unicode?

标签 python string python-2.7 unicode

我对 python 的 unicode/str 进程感到困惑。我在python2中遇到过一些情况。

下面这句话是在IDE pycharm中用utf8编码写在py文件中的。

  1. print "hello!%s"% u"中国"
  2. print "hello!%s"% "中国"
  3. print u"hello!%s"% "中国"

仅情况3引发解码错误:

UnicodeDecodeError: 'ascii' codec can't decode byte 0xe4 in position 0: ordinal not in range(128).

有人能告诉我Python是如何处理这句话的吗?为什么会有这样的结果?

最佳答案

如果删除打印语句,您可以看到更多详细信息:

>>> "hello! %s" % u"中国"
u'hello! \u4e2d\u56fd'
>>> "hello! %s" % "中国"
'hello! \xe4\xb8\xad\xe5\x9b\xbd'
>>> u"hello! %s" % "中国"
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe4 in position 0: ordinal not in range(128)

这给了我们线索。每当涉及到任何 unicode 字符串时,Python 都会尝试将另一端转换为 unicode;并且,像往常一样,如果没有任何相反的指示,它将始终假定编码是 ASCII。

在第一种情况下,它尝试将“hello”字节串转换为unicode;由于没有非 ASCII 字符,因此可以正常工作,并且可以安全地使用现有的 unicode 字符串对结果进行插值。

在第二种情况下,两边都是字节串,因此不尝试转换;结果仍然是一个字节串。

在第三种情况下,“hello”已经是unicode,因此它尝试转换另一端;但由于这些是非 ASCII 字符,因此失败。但是,直接指定编码确实有效:

>>> u"hello! %s" % "中国".decode('utf-8')
u'hello! \u4e2d\u56fd'

关于python - python2内部如何处理字符串和unicode?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35792962/

相关文章:

python - 如何在函数参数中留出空格?

django - 由于 egg_info 错误,无法通过 pip 安装

python - 通过 Fabric 进行 npm 安装

将十六进制数字转换为十进制以进行比较 (C)

python-2.7 - Wagtail Docker 六包冲突升级到 wagtail 1.7

c# - 用另一个替换字符串的一部分

python - 从字符串中删除所有出现的多个字符

python - 如何使用 Python 测试唯一字符串和重复字符串(不同大小写)

python - 从 BeautifulSoup 结果中获取表单 "action"

python - 算法:类(class)顺序