Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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

1 Answer

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...