Programmatically discovering encoding types supported by codecs module

python at bdurham.com python at bdurham.com
Wed Mar 24 13:55:32 EDT 2010


Benjamin,

> According to my brief messing around with the REPL, encodings.aliases.aliases is a good place to start. I don't know of any way to get the Language column, but at the very least that will give you most of the supported encodings and any aliases they have.

Thank you - that's exactly the type of information I was looking for.

I'm including the following for anyone browsing the mailing list
archives in the future.

Here's the snippet we're using to dynamically generate the codec
documentation posted on the docs.python website.

import encodings
encodingDict = encodings.aliases.aliases
encodingType = dict()
for key, value in encodingDict.items():
    if value not in encodingType:
        encodingType[ value ] = list()
    encodingType[ value ].append( key )

for key in sorted( encodingType.keys() ):
    aliases = sorted( encodingType[ key ] )
    aliases = ', '.join( aliases )
    print '%-20s%s' % ( key, aliases )

Regards,
Malcolm



More information about the Python-list mailing list