-1

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?

The_Fishy
  • 143
  • 10
  • 3
    Can you try with `f = open("file.txt", 'w', encoding='utf-8')`? ` – Abdul Niyas P M Dec 27 '22 at 14:37
  • 2
    Does this answer your question? [Writing on text file, accents and special characters not displaying correctly](https://stackoverflow.com/questions/33761993/writing-on-text-file-accents-and-special-characters-not-displaying-correctly) – Pranav Hosangadi Dec 27 '22 at 14:46

2 Answers2

1

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.

Arifa Chan
  • 947
  • 2
  • 6
  • 23
-3
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
Rich
  • 6,470
  • 15
  • 32
  • 53