python - 替代 u'string'

标签 python utf-8

我以 UTF-8 编码保存我的脚本。

我将 Windows 上的代码页更改为 65001。

我在 python 2.6 上

脚本#1

# -*- coding: utf-8 -*-
print u'Español'
x = raw_input()

脚本 #2

# -*- coding: utf-8 -*-
a = 'Español'
a.encode('utf8')
print a
x = raw_input()

脚本 #1,打印单词 fine 没有错误,脚本 #2 出错:

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

我希望能够像在脚本 #2 中那样动态地打印这个变量而不会出错。 我提到 encode('utf8') 相当于执行 u'string'

显然,这不是因为它会抛出错误。 伙计们,我该怎么做?

最佳答案

将您的代码更改为以下内容:

# -*- coding: utf-8 -*-
a = 'Español'
a = a.decode('utf8')
print a
x = raw_input()

解码指定应如何读取 字符串,并返回值。进行上述更改应该可以解决您的问题。

问题在于 python 将字符串存储为字节列表,而不管文件的编码如何。重要的是如何读取这些字节,这就是我们在使用 decode()u'' 时正在做的事情。

关于python - 替代 u'string',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3577561/

相关文章:

python - 使用 Github Actions 安装 Poetry,未找到诗歌命令。无法将诗歌添加到路径中

mysql - mysql 字符集和 GWT 的问题

windows - 为什么通过管道传递我的命令 | % {echo "$_"} 让 UTF-8 工作?

c++ - 如何正确处理大小 >= 2B 的渲染 utf-8 字符?

python - 如何按月屏蔽数据框?

python - 如何检查 Python 脚本已经生成的字符串的 linux shell 编码

c# - 无法以正确的格式对特殊字符电子邮件进行编码,gmail api & ae.net.mail

mysqldump 以错误的字符集导出数据

python - 优化比较次数

python - 将图例中的 Matplotlib 矩形更改为圆形