import chardet
rawdata = open(infile, 'r').read()
result = chardet.detect(rawdata)
charenc = result['encoding']
Character detection in a text file in Python using the Universal Encoding Detector (chardet)