gpt4 book ai didi

python - 替代 u'string'

转载 作者:太空宇宙 更新时间:2023-11-04 06:51:19 25 4
gpt4 key购买 nike

我以 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com