[英]Spanish text in .py files
This is the code这是代码
A = "Diga sí por cualquier número de otro cuidador.".encode("utf-8")
I get this error:我收到此错误:
'ascii' codec can't decode byte 0xed in position 6: ordinal not in range(128) “ascii”编解码器无法解码 position 6 中的 0xed 字节:序数不在范围内(128)
I tried numerous encodings unsuccessfully.我尝试了许多编码都没有成功。
Edit:编辑:
I already have this at the beginning我一开始就已经有了
# -*- coding: utf-8 -*-
Changing to更改为
A = u"Diga sí por cualquier número de otro cuidador.".encode("utf-8")
doesn't help没有帮助
Are you using Python 2?您使用的是 Python 2 吗?
In Python 2, that string literal is a bytestring.在 Python 2 中,该字符串文字是一个字节串。 You're trying to encode it, but you can encode only a Unicode string, so Python will first try to decode the bytestring to a Unicode string using the default "ascii" encoding.
您正在尝试对其进行编码,但您只能对 Unicode 字符串进行编码,因此 Python 将首先尝试使用“默认”编码将字节字符串解码为 Unicode
Unfortunately, your string contains non-ASCII characters, so it can't be decoded to Unicode.不幸的是,您的字符串包含非 ASCII 字符,因此无法解码为 Unicode。
The best solution is to use a Unicode string literal, like this:最好的解决方案是使用 Unicode 字符串文字,如下所示:
A = u"Diga sí por cualquier número de otro cuidador.".encode("utf-8")
Error message: 'ascii' codec can't decode byte 0xed in position 6: ordinal not in range(128)
错误消息:
'ascii' codec can't decode byte 0xed in position 6: ordinal not in range(128)
says that the 7th byte is 0xed
.表示第 7 个字节是
0xed
。 This is either the first byte of the UTF-8 sequence for some (maybe CJK) high-ordinal Unicode character (that's absolutely not consistent with the reported facts), or it's your i-acute encoded in Latin1 or cp1252.对于某些(可能是 CJK)高序数 Unicode 字符(这与报告的事实绝对不一致),这是 UTF-8 序列的第一个字节,或者它是您用 Latin1 或 cp1252 编码的 i-acute。 I'm betting on the cp1252.
我赌的是cp1252。
If your file was encoded in UTF-8, the offending byte would be not 0xed
but 0xc3
:如果您的文件以 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 ####
Having # -*- coding: utf-8 -*-
at the start of your code does not magically ensure that your file is encoded in UTF-8 -- that's up to you and your text editor. # -*- coding: utf-8 -*-
在您的代码开头并不能神奇地确保您的文件以 UTF-8 编码 - 这取决于您和您的文本编辑器。
Actions:行动:
put on first line of your code this:把你的代码的第一行:
# -*- coding: utf-8 -*-
In the first or second line of your code, type the comment:在代码的第一行或第二行中,输入注释:
# -*- coding: latin-1 -*-
For a list of symbols supported see: http://en.wikipedia.org/wiki/Latin-1_Supplement_%28Unicode_block%29有关支持的符号列表,请参阅: http://en.wikipedia.org/wiki/Latin-1_Supplement_%28Unicode_block%29
And the languages covered: http://en.wikipedia.org/wiki/ISO_8859-1涵盖的语言: http://en.wikipedia.org/wiki/ISO_8859-1
Maybe this is what you want to do:也许这就是你想要做的:
A = 'Diga sí por cualquier número de otro cuidador'.decode('latin-1')
And don't forget to add # -*- coding: latin-1 -*-
at the beginning of your code.并且不要忘记在代码的开头添加
# -*- coding: latin-1 -*-
。
You should specify your source file's encoding by adding the following line to the very beginning of your code (assuming that your file is encoded in UTF-8):您应该通过将以下行添加到代码的最开始来指定源文件的编码(假设您的文件以 UTF-8 编码):
# Encoding: UTF-8
Otherwise, Python will assume an ASCII encoding and fail during parsing.否则,Python 将采用 ASCII 编码并在解析过程中失败。
You probably operate on normal string, not unicode string:您可能对普通字符串进行操作,而不是 unicode 字符串:
>> type(u"zażółć gęślą jaźń")
-> <type 'unicode'>
>> type("zażółć gęślą jaźń")
-> <type 'str'>
so所以
u"Diga sí por cualquier número de otro cuidador.".encode("utf-8")
should work.应该管用。
If you want use unicode strings by default, put如果你想默认使用 unicode 字符串,把
# -*- coding: utf-8 -*-
in the first line of your script.在脚本的第一行。
Look also in docs .另请参阅 docs 。
PS It's Polish in examples above:) PS 在上面的例子中它是波兰语:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.