Opening multiple Files in Different Encoding

Steven D'Aprano steve+comp.lang.python at
Wed Jul 11 08:22:02 CEST 2012

On Tue, 10 Jul 2012 10:46:08 -0700, Subhabrata wrote:

> Dear Group,
> I kept a good number of files in a folder. Now I want to read all of
> them. They are in different formats and different encoding. Using
> listdir/glob.glob I am able to find the list but how to open/read or
> process them for different encodings?

open('first file', encoding='uft-8')
open('second file', encoding='latin1')

How you decide which encoding to use is up to you. Perhaps you can keep a 
mapping of {filename: encoding} somewhere.

Or perhaps you can try auto-detecting the encodings. The chardet module 
should help you there.


More information about the Python-list mailing list