首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何用Python创建GSM-7编码?

如何用Python创建GSM-7编码?
EN

Stack Overflow用户
提问于 2016-06-12 14:12:31
回答 1查看 1.9K关注 0票数 1

GM-7字符集定义为基本映射表+扩展字符映射表(03.38).这意味着u'@'应该映射到b'\x00' (长度为1的字节字符串),而u'['应该映射到b'\x1b<'b'\x1b\x3c' (长度为2的字节字符串)。

我已经通过扩展encoding_table成功地使编码部分工作起来,但是我不知道如何使用decoding_table..?

以下是完整的编解码器样板:

代码语言:javascript
复制
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)

以下是表格的定义:

代码语言:javascript
复制
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'

编码部分现在工作,但解码没有:

代码语言:javascript
复制
>>> u'['.encode('g7')
'\x1b<'
>>> _.decode('g7')
u'\x1b<'
>>>

我还没有找到关于编写编码的文档的好来源。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-06-12 15:06:04

基于@Martijn Pieters的评论,我将代码更改为:

代码语言:javascript
复制
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)

似乎行得通:)

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37775209

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档