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?
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
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.
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?
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
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.