简体   繁体   English

自定义Python Charmap编解码器

[英]Custom Python Charmap Codec

I'm trying to write a custom Python codec. 我正在尝试编写自定义Python编解码器。 Here's a short example: 这是一个简短的示例:

import codecs

class TestCodec(codecs.Codec):
    def encode(self, input_, errors='strict'):
        return codecs.charmap_encode(input_, errors, {
            'a': 0x01,
            'b': 0x02,
            'c': 0x03,
        })

    def decode(self, input_, errors='strict'):
        return codecs.charmap_decode(input_, errors, {
            0x01: 'a',
            0x02: 'b',
            0x03: 'c',
        })

def lookup(name):
    if name != 'test':
        return None
    return codecs.CodecInfo(
        name='test',
        encode=TestCodec().encode,
        decode=TestCodec().decode,
    )

codecs.register(lookup)
print(b'\x01\x02\x03'.decode('test'))
print('abc'.encode('test'))

Decoding works, but encoding throws an exception: 解码有效,但是编码会引发异常:

$ python3 codectest.py
abc
Traceback (most recent call last):
  File "codectest.py", line 29, in <module>
    print('abc'.encode('test'))
  File "codectest.py", line 8, in encode
    'c': 0x03,
UnicodeEncodeError: 'charmap' codec can't encode characters in position 0-2:
character maps to <undefined>

Any ideas how to use charmap_encode properly? 任何想法如何正确使用charmap_encode

Look at https://docs.python.org/3/library/codecs.html#encodings-and-unicode (third paragraph): 查看https://docs.python.org/3/library/codecs.html#encodings-and-unicode (第三段):

There's another group of encodings (the so called charmap encodings) that choose a different subset of all Unicode code points and how these code points are mapped to the bytes 0x0 - 0xff . 还有另一组选择所有Unicode代码点的不同子集,并且这些代码点是如何映射到字节编码(所谓的字符表编码)的0x0 - 0xff To see how this is done simply open eg encodings/cp1252.py (which is an encoding that is used primarily on Windows). 要查看如何完成此操作,只需打开例如encodings/cp1252.py (这是主要在Windows上使用的编码)即可。 There's a string constant with 256 characters that shows you which character is mapped to which byte value. 有一个包含256个字符的字符串常量,向您显示哪个字符映射到哪个字节值。

take the hint to look at encodings/cp1252.py, and check out the following code: 提示看encodings / cp1252.py,并检查以下代码:

import codecs

class TestCodec(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)

def lookup(name):
    if name != 'test':
        return None
    return codecs.CodecInfo(
        name='test',
        encode=TestCodec().encode,
        decode=TestCodec().decode,
    )

decoding_table = (
    'z'
    'a'
    'b'
    'c'
)    
encoding_table=codecs.charmap_build(decoding_table)
codecs.register(lookup)

### --- following is test/debug code
print(ascii(encoding_table))

print(b'\x01\x02\x03'.decode('test'))
foo = 'abc'.encode('test')
print(ascii(foo))

Output: 输出:

{97: 1, 122: 0, 99: 3, 98: 2}
abc
b'\x01\x02\x03'

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 涉及&#39;charmap&#39;编解码器的Python UnicodeEncodeError - Python UnicodeEncodeError involving 'charmap' codec charmap编解码器无法使用python中的gzip压缩文件解码字节错误 - charmap codec can't decode byte error with gzipped file in python Python UnicodeEncodeError:使用 GEOPY 时无法编码“charmap”编解码器 - Python UnicodeEncodeError: 'charmap' codec can't encode when using GEOPY Python,UnicodeEncodeError:“ charmap”编解码器无法在位置编码字符 - Python, UnicodeEncodeError: 'charmap' codec can't encode characters in position MongoDB中的Python错误“ UnicodeEncodeError:&#39;charmap&#39;编解码器无法编码字符” - Python error “UnicodeEncodeError: 'charmap' codec can't encode character” in MongoDB Python Dataframe 到 CSV - UnicodeEncodeError: 'charmap' 编解码器无法编码字符 - Python Dataframe to CSV - UnicodeEncodeError: 'charmap' codec can't encode characters Python-&#39;charmap&#39;编解码器无法编码字符&#39;\\ xe3&#39; - Python - 'charmap' codec can't encode character '\xe3' Python编码NLTK-&#39;charmap&#39;编解码器无法编码字符 - Python Encoding NLTK - 'charmap' codec can't encode character Python Selenuim - UnicodeEncodeError 'charmap' 编解码器无法编码 - Python Selenuim - UnicodeEncodeError 'charmap' codec can't encode python错误codecs.charmap_encode(input,errors,encoding_map)UnicodeEncodeError:&#39;charmap&#39;编解码器无法编码字符 - python error codecs.charmap_encode(input,errors,encoding_map) UnicodeEncodeError: 'charmap' codec can't encode characters
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM