.py文件中的西班牙语文本

7 投票
7 回答
9939 浏览
提问于 2025-04-16 18:37

这是代码

A = "Diga sí por cualquier número de otro cuidador.".encode("utf-8")

我遇到了这个错误:

'ascii' 编码无法解码位置 6 的字节 0xed:序号不在范围内(128)

我尝试了很多种编码方式,但都没有成功。

编辑:

我在开头已经有这个

# -*- coding: utf-8 -*-

改成

A = u"Diga sí por cualquier número de otro cuidador.".encode("utf-8")

也没有帮助

7 个回答

1

在你的代码的第一行加上这个:

# -*- coding: utf-8 -*-
3

错误信息:'ascii' 编码无法解码位置 6 的字节 0xed:序号不在范围内(128)

这句话的意思是,第七个字节是0xed。这可能是某个高Unicode字符(可能是中文、日文或韩文)的UTF-8序列的第一个字节(这和报告的情况完全不符),或者是你用Latin1或cp1252编码的i-acute字符。我觉得更可能是cp1252编码。

如果你的文件是用UTF-8编码的,那么出问题的字节应该不是0xed,而是0xc3

Preliminaries:
>>> import unicodedata
>>> unicodedata.name(u'\xed')
'LATIN SMALL LETTER I WITH ACUTE'
>>> uc = u'Diga s\xed por'

What happens if file is encoded in UTF-8:
>>> infile = uc.encode('utf8')
>>> infile
'Diga s\xc3\xad por'
>>> infile.encode('utf8')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 6: ordinal not in range(128)
#### NOT the message reported in the question ####

What happens if file is encoded in cp1252 or latin1 or similar:
>>> infile = uc.encode('cp1252')
>>> infile
'Diga s\xed por'
>>> infile.encode('utf8')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeDecodeError: 'ascii' codec can't decode byte 0xed in position 6: ordinal not in range(128)
#### As reported in the question ####

在你的代码开头加上# -*- coding: utf-8 -*-并不能自动保证你的文件是UTF-8编码的——这要靠你和你的文本编辑器来决定。

建议的操作:

  1. 把你的文件保存为UTF-8编码。
  2. 正如其他人所建议的,你需要用u'blah blah'的格式。
4

你在用Python 2吗?

在Python 2中,那个字符串是字节串。你想对它进行编码,但其实只能对Unicode字符串进行编码,所以Python会先尝试用默认的“ascii”编码把字节串解码成Unicode字符串。

可惜的是,你的字符串里有一些非ASCII字符,所以它不能被解码成Unicode。

最好的解决办法是使用Unicode字符串字面量,像这样:

A = u"Diga sí por cualquier número de otro cuidador.".encode("utf-8")

撰写回答