Skip to main content
 首页 » 编程设计

python之替代 u'string'

2025年05月04日9kuangbin

我以 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'' 时正在做的事情。