栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

自定义Python Charmap编解码器

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

自定义Python Charmap编解码器

查看https://docs.python.org/3/library/precs.html#encodings-and-
unipre
(第三段):

还有另一组编码(所谓的charmap编码),它们选择所有Unipre代码点的不同子集,以及如何将这些代码点映射到字节

0x0
-
0xff
。要查看如何完成此操作,只需打开eg
encodings/cp1252.py
(这是主要在Windows上使用的编码)。有一个包含256个字符的字符串常量,向您显示哪个字符映射到哪个字节值。

提示看encodings / cp1252.py,并查看以下代码:

import precsclass TestCodec(precs.Codec):    def enpre(self, input_, errors='strict'):        return precs.charmap_enpre(input_, errors, encoding_table)    def depre(self, input_, errors='strict'):        return precs.charmap_depre(input_, errors, decoding_table)def lookup(name):    if name != 'test':        return None    return precs.CodecInfo(        name='test',        enpre=TestCodec().enpre,        depre=TestCodec().depre,    )decoding_table = (    'z'    'a'    'b'    'c')    encoding_table=precs.charmap_build(decoding_table)precs.register(lookup)### --- following is test/debug preprint(ascii(encoding_table))print(b'x01x02x03'.depre('test'))foo = 'abc'.enpre('test')print(ascii(foo))

输出:

{97: 1, 122: 0, 99: 3, 98: 2}abcb'x01x02x03'


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/623618.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号