Wednesday, April 13, 2011

Detect whether charset exists in python

Is it possible to check in Python whether a given charset exists/is installed. For example:
check('iso-8859-1') -> True
check('bla') -> False

From stackoverflow
  • You can use the lookup() function in the codecs module. It throws an exception if a codec does not exist:

    import codecs
    def exists_encoding(enc):
        try:
            codecs.lookup(enc)
        except LookupError:
            return False
        return True
    exists_encoding('latin1')
    

0 comments:

Post a Comment