Convert from UTF16 LE to ANSI in Python

528 Views Asked by At

I have several files encoded in UTF-16LE and I want to convert them to ANSI. I found some suggestions on stack overflow (Convert from ANSI to UTF-8) but this doesn't work. That is that I can convert files but there are spaces between the words and the numbers and this character from the conversion: ÿ þ

import glob
import codecs

for each in glob.glob('path/**/*.txt', recursive=True):

#read input file
with codecs.open(each, 'r', encoding = 'mbcs') as file:
     lines = file.read()
     
#write output file
with codecs.open(each, 'w', encoding = 'UTF-16LE') as file:
     file.write(lines)

What I am missing? Thanks

0

There are 0 best solutions below