Skip to content Skip to sidebar Skip to footer

How To Create A Gsm-7 Encoding In Python?

The GSM-7 character set is defined as a basic mapping table + an extension character mapping table (https://en.wikipedia.org/wiki/GSM_03.38#GSM_7-bit_default_alphabet_and_extension

Solution 1:

Based on @Martijn Pieters comment, I've changed the code to:

defdecode_gsm7(txt, errors):
    ext_table = {
        '\x40': u'|',
        '\x14': u'^',
        '\x65': u'€',
        '\x28': u'{',
        '\x29': u'}',
        '\x3C': u'[',
        '\x3D': u'~',
        '\x3E': u']',
        '\x2F': u'\\',
    }
    chunks = filter(None, txt.split('\x1b'))  # split on ESC
    res = u''for chunk in chunks:
        res += ext_table[chunk[0]]  # first character after ESCiflen(chunk) > 1:
            # charmap_decode returns a tuple..
            decoded, _ = codecs.charmap_decode(chunk[1:], errors, decoding_table)
            res += decoded
    return res, len(txt)


classGSM7Codec(codecs.Codec):

    defencode(self, txt, errors='strict'):
        return codecs.charmap_encode(txt, errors, encoding_table)

    defdecode(self, txt, errors='strict'):
        return decode_gsm7(txt, errors)

which seems to work :-)

Post a Comment for "How To Create A Gsm-7 Encoding In Python?"