【问题标题】:How to create a GSM-7 encoding in Python?如何在 Python 中创建 GSM-7 编码?
【发布时间】:2016-06-12 23:06:04
【问题描述】:

GSM-7字符集定义为基本映射表+扩展字符映射表(https://en.wikipedia.org/wiki/GSM_03.38#GSM_7-bit_default_alphabet_and_extension_table_of_3GPP_TS_23.038_.2F_GSM_03.38)。 这意味着u'@' 应该映射到b'\x00'(长度为1 的字节串),但u'[' 应该映射到b'\x1b<' 或b'\x1b\x3c'(长度为2 的字节串)。

我已经设法通过扩展encoding_table 使编码部分工作,但我不确定如何使用decoding_table..?

为了完整起见,这里是编解码器样板:

import codecs
from encodings import normalize_encoding

class GSM7Codec(codecs.Codec):
    def encode(self, input, errors='strict'):
        return codecs.charmap_encode(input, errors, encoding_table)

    def decode(self, input, errors='strict'):
        return codecs.charmap_decode(input, errors, decoding_table)

class GSM7IncrementalEncoder(codecs.IncrementalEncoder):
    def encode(self, input, final=False):
        return codecs.charmap_encode(input, self.errors, encoding_table)[0]

class GSM7IncrementalDecoder(codecs.IncrementalDecoder):
    def decode(self, input, final=False):
        return codecs.charmap_decode(input, self.errors, decoding_table)[0]

class GSM7StreamWriter(codecs.Codec, codecs.StreamWriter): pass
class GSM7StreamReader(codecs.Codec, codecs.StreamReader): pass

_cache = {}

def search_function(encoding):
    """Register the gsm-7 encoding with Python's codecs API. This involves
       adding a search function that takes in an encoding name, and returns
       a codec for that encoding if it knows one, or None if it doesn't.
    """
    if encoding in _cache:
        return _cache[encoding]
    norm_encoding = normalize_encoding(encoding)
    if norm_encoding in ('gsm_7', 'g7', 'gsm7'):
        cinfo = codecs.CodecInfo(
            name='gsm-7',
            encode=GSM7Codec().encode,
            decode=GSM7Codec().decode,
            incrementalencoder=GSM7IncrementalEncoder,
            incrementaldecoder=GSM7IncrementalDecoder,
            streamreader=GSM7StreamReader,
            streamwriter=GSM7StreamWriter,
        )
        _cache[norm_encoding] = cinfo
        return cinfo
    return None

codecs.register(search_function)

这里是表定义:

decoding_table = (
    u"@£$¥èéùìòÇ\nØø\rÅå" +
    u"Δ_ΦΓΛΩΠΨΣΘΞ\x1bÆæßÉ" +
    u" !\"#¤%&'()*+,-./" +
    u"0123456789:;<=>?" +
    u"¡ABCDEFGHIJKLMNO" +
    u"PQRSTUVWXYZÄÖÑܧ" +
    u"¿abcdefghijklmno" +
    u"pqrstuvwxyzäöñüà"
)

encoding_table = codecs.charmap_build(
    decoding_table + '\0' * (256 - len(decoding_table))
)

# extending the encoding table with extension characters
encoding_table[ord(u'|')] = '\x1b\x40'
encoding_table[ord(u'^')] = '\x1b\x14'
encoding_table[ord(u'€')] = '\x1b\x65'
encoding_table[ord(u'{')] = '\x1b\x28'
encoding_table[ord(u'}')] = '\x1b\x29'
encoding_table[ord(u'[')] = '\x1b\x3C'
encoding_table[ord(u'~')] = '\x1b\x3D'
encoding_table[ord(u']')] = '\x1b\x3E'
encoding_table[ord(u'\\')] = '\x1b\x2F'

编码部分现在可以工作,但解码不工作:

>>> u'['.encode('g7')
'\x1b<'
>>> _.decode('g7')
u'\x1b<'
>>>

我一直找不到关于编写编码的良好文档来源。

【问题讨论】:

  • 这里不能使用表格进行解码。您必须创建自己的函数来在此处进行解码(逐个字符处理以查找多字节序列)。
  • 感谢@MartijnPieters 我根据您的评论创建了一个答案。

标签: python encoding gsm


【解决方案1】:

根据@Martijn Pieters 的评论,我将代码更改为:

def decode_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 ESC
        if len(chunk) > 1:
            # charmap_decode returns a tuple..
            decoded, _ = codecs.charmap_decode(chunk[1:], errors, decoding_table)
            res += decoded
    return res, len(txt)


class GSM7Codec(codecs.Codec):

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

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

这似乎有效:-)

【讨论】:

  • 在GSM7Codec.encode 中,codecs.charmap_encode 是用从未定义的encoding_table 调用的。
  • 我的错!感谢您的回复
猜你喜欢
  • 2011-12-27
  • 1970-01-01
  • 1970-01-01
  • 2012-10-19
  • 2011-01-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-25
相关资源
最近更新 更多