[英]Reading a file with french characters in Python
I would like to read a file that contains french characters in Python such "é".I'm using these lines of code to do that:我想在 Python 中读取一个包含法语字符的文件,例如“é”。我正在使用这些代码行来做到这一点:
import codecs
with codecs.open(r'C:\Users\chsafouane\Desktop\saf.txt', encoding='ascii') as f:
for line in f.readlines():
line
Yet, I get a然而,我得到一个
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe9 in position 3: ordinal not in range(128)
To reproduce the error, the file I'm trying to read contains only one word: "Accélération".为了重现错误,我试图阅读的文件只包含一个词:“加速”。 Is there a way to accomplish this?有没有办法做到这一点?
For a fie including only this word "Accélération", utf-8 encodinf doesn't work and it returns the following error对于仅包含“加速”这个词的 fie,utf-8 编码不起作用并返回以下错误
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe9 in position 3: invalid continuation byte
As proposed by @sciroccorics, latin-1
encoding works well and it returns the right word.正如@sciroccorics 所建议的那样, latin-1
编码效果很好并且它返回了正确的单词。 So the chunk of code that works is the following:因此,有效的代码块如下:
import codecs
with codecs.open(r'C:\Users\chsafouane\Desktop\saf.txt', encoding='latin1') as f:
for line in f.readlines():
print(line)
尝试“cp1252”,对我来说适用于所有法语字符
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.