UnicodeDecodeError:'ascii'编解码器无法解码 [英] UnicodeDecodeError: 'ascii' codec can't decode
问题描述
我正在用file.readline()在Python中读取包含罗马尼亚语的文件。
由于编码,我遇到许多字符的问题。
I'm reading a file that contains Romanian words in Python with file.readline(). I've got problem with many characters because of encoding.
示例:
>>> a = "aberație" #type 'str'
>>> a -> 'abera\xc8\x9bie'
>>> print sys.stdin.encoding
UTF-8
我已经尝试了utf-8,cp500等,但它不起作用。
I've tried encode() with utf-8, cp500 etc, but it doesn't work.
我找不到哪个是正确的字符编码我必须使用?
I can't find which is the right Character encoding I have to use ?
提前感谢。
编辑:目的是将文件中的单词存储在词典中,并在打印时将其打印到
The aim is to store the word from file in a dictionnary, and when printing it, to obtain aberație and not 'abera\xc8\x9bie'
推荐答案
你想做什么?
这是一组字节:
BYTES = 'abera\xc8\x9bie'
这是一组代表一个 utf-8
字符串aberaţie的编码。您解码以获取您的unicode字符串的字节:
It's a set of bytes which represents a utf-8
encoding of the string "aberație". You decode the bytes to get your unicode string:
>>> BYTES
'abera\xc8\x9bie'
>>> print BYTES
aberație
>>> abberation = BYTES.decode('utf-8')
>>> abberation
u'abera\u021bie'
>>> print abberation
aberație
如果要将unicode字符串存储到文件中,那么你必须将编码为您选择的特定字节格式:
If you want to store the unicode string to a file, then you have to encode it to a particular byte format of your choosing:
>>> abberation.encode('utf-8')
'abera\xc8\x9bie'
>>> abberation.encode('utf-16')
'\xff\xfea\x00b\x00e\x00r\x00a\x00\x1b\x02i\x00e\x00'
这篇关于UnicodeDecodeError:'ascii'编解码器无法解码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!