[Python-Dev] Quick sum up about open() + BOM

Victor Stinner victor.stinner at haypocalc.com
Sat Jan 9 14:34:17 CET 2010

Le samedi 09 janvier 2010 02:12:28, MRAB a écrit :
> What about listing the possible encodings? It would try each in turn
> until it found one where the BOM matched or had no BOM:
>      my_file = open(filename, 'r', encoding='UTF-8-sig|UTF-16|UTF-8')
> or is that taking it too far?

Yes, you're taking it foo far :-) Checking BOM is reliable, whereas *guessing* 
the charset only using the byte stream can only be an heuristic. Guess a 
charset is a complex problem, they are 3rd party library to do that, like the 
chardet project.

Victor Stinner

More information about the Python-Dev mailing list