I recently rediscovered this strange behaviour in Python’s Unicode handling.—Evan

Please note I am saying only that something like this may want to me considered for addition to the documentation, and not to the Python standard library. This example function more closely replicates the logic that is used on those Windows applications when opening ".txt" files. It uses the default locale if there is no BOM:

    def autodecode( s ):        if s.beginswith( codecs.BOM_UTF8 ):            # The byte string s is UTF-8            out = s.decode( "utf8" )            return out[1:]        else: return s.decode()—Evan