Search code examples
pythonencodingansiutf-16

Best way for Parsing ANSI and UTF-16LE files using Python 2/3?


I have a collection of files encoded in ANSI or UTF-16LE. I would like python to open the files using the correct encoding. The problem is that the ANSI files do not raise any sort of exception when encoded using UTF-16le and vice versa.

Is there a straightforward way to open up the files using the correct file encoding?


Solution

  • Use the chardet library to detect the encoding.