Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
316 views
in Technique[技术] by (71.8m points)

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:

import codecs
with codecs.open(r'C:UserschsafouaneDesktopsaf.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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

For a fie including only this word "Accélération", utf-8 encodinf doesn't work and it returns the following error

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. So the chunk of code that works is the following:

import codecs
with codecs.open(r'C:UserschsafouaneDesktopsaf.txt', encoding='latin1') as f:
    for line in f.readlines():
        print(line)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...