Module iconv_codecs
[hide private]
[frames] | no frames]

Module iconv_codecs

source code

iconv_codec: module to register python codecs to encode/decode any char supported by system's iconv command.

Usage:

iconv supports codecs unsupported by python:

>>> u'testing'.encode('ansi_x3.110-1983')
Traceback (most recent call last):
...
LookupError: unknown encoding: ansi_x3.110-1983
>>> import iconv_codecs
>>> 'ansi_x3.110-1983' in iconv_codecs.get_supported_codecs()
True

Just register the codec you want:

>>> iconv_codecs.register('ansi_x3.110-1983')

Then you can use it:

>>> u'testing'.encode('ansi_x3.110-1983')
'testing'

If you want to force iconv usage for an encoding already supported by python, just use the encoding name with an 'iconv:' prefix (no need to register):

>>> '\x87'.decode('iconv:CP860')
u'\xe7'

To register all python unsupported codecs, just call register() without parameters:

>>> iconv_codecs.register()
>>> u'\xe7'.encode('utf32')
'\xff\xfe\x00\x00\xe7\x00\x00\x00'

That will poll iconv for a list of codecs it supports and register the ones python doesn't support already.

The module will look for iconv in the path. If you need a different iconv location just set it:

>>> iconv_codecs.ICONV_EXECUTABLE = '/usr/bin/iconv'
Functions [hide private]
 
_get_unregistered_codecs()
Returns a list of iconv codecs that aren't supported by python directly
source code
 
register(*codecs)
Register the codecs passed for iconv usage.
source code
 
get_supported_codecs()
Returns a list of iconv supported codecs
source code
 
_run_iconv(from_codec, to_codec, extra_params=None) source code
 
_iconv_factory(codec_name) source code
Variables [hide private]
  ICONV_EXECUTABLE = 'iconv'
change this to reflect your installation path
  _codecs = set([])
Global with the names of registered codecs
Function Details [hide private]

register(*codecs)

source code 

Register the codecs passed for iconv usage. Codecs previously registered will be unregistered.

>>> import iconv_codecs
>>> iconv_codecs.register('ansi_x3.110-1983')

Then you can use it:

>>> u'testing'.encode('ansi_x3.110-1983')
'testing'

If you want to register all codecs not already supported by python, just suppress all arguments:

>>> iconv_codecs.register()