Here are 2 code samples, Python3 : the first one writes two files with latin1 encoding :
s='On écrit ça dans un fichier.'
with open('spam1.txt', 'w',encoding='ISO-8859-1') as f:
print(s, file=f)
with open('spam2.txt', 'w',encoding='ISO-8859-1') as f:
f.write(s)
The second one reads the same files with the same encoding :
with open('spam1.txt', 'r',encoding='ISO-8859-1') as f:
s1=f.read()
with open('spam2.txt', 'r',encoding='ISO-8859-1') as f:
s2=f.read()
Now, printing s1 and s2 I get
On écrit ça dans un fichier.
instead of the initial "On écrit ça dans un fichier."
What is wrong ? I also tried with io.open but I miss something. The funny part is that I had no such problem with Python2.7 and its str.decode method which is now gone...
Could someone help me ?
>>> 'On écrit ça dans un fichier.'.encode('utf8').decode('latin1')
gives'On écrit ça dans un fichier.'
– Galley