I am trying to open and change several text files. My files are in 'Latin-1' but when I use f.read
all the letters with accents are converted into "ã".
My code is:
for dname, dirs, files in os.walk("mydirection"):
for fname in files:
fpath = os.path.join(dname, fname)
with open(fpath, encoding='latin-1') as f:
text = f.read()
text = text.replace(r'- ', '')
# remove punctuation
text = re.sub(r'[^\w\s]', ' ', text)
with open(fpath, 'w', encoding='latin-1') as file:
file.write(text)
Is it possible to write and change the text files and keep them in 'Latin-1'?
Example of text file: "élève,"
What I want: "élève" (or if it is not possible "eleve")
What I am obtaining: "ã l ã ve"