Python print french characters to another file

Question:

Whenever I print a french character (for example "é") in another file using python, it changes it to a weird character.

program.py:

f = open("file.txt", 'w')
print("é, è, ç", file=f)

file.txt:

�, �, �

So please does anyone know how to print out french characters to any other files as they are?

Asked By: The_Fishy

||

Answers:

def wordlist(filename):
    f = open(filename, mode='r')
    text = f.read()
    print(text)
    wordlist = [fixCaps(w) for w in re.findall(r"[w']+|[.,!?;]", text)]
    print(wordlist)
    f.close()
    return wordlist
Answered By: Devansh Gupta

You can try using UTF-8 encoding:

f = open("file.txt", 'w', encoding='utf-8')
print("é, è, ç", file=f)

# é, è, ç

The default encoding is platform dependent, but any encoding supported
by Python can be passed. See the codecs module for the list of
supported encodings.

Answered By: Arifa Chan
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.