python construct_Python construct包_程序模块 - PyPI - Python中文网

示例

Struct是有序命名字段的集合:>>> format = Struct(

... "signature" / Const(b"BMP"),

... "width" / Int8ub,

... "height" / Int8ub,

... "pixels" / Array(this.width * this.height, Byte),

... )

>>> format.build(dict(width=3,height=2,pixels=[7,8,9,11,12,13]))

b'BMP\x03\x02\x07\x08\t\x0b\x0c\r'

>>> format.parse(b'BMP\x03\x02\x07\x08\t\x0b\x0c\r')

Container(signature=b'BMP')(width=3)(height=2)(pixels=[7, 8, 9, 11, 12, 13])

Sequence是有序字段的集合,不同于Array和GreedyRange,因为这两个字段是同质的:>>> format = Sequence(PascalString(Byte, "utf8"), GreedyRange(Byte))

>>> format.build([u"lalaland", [255,1,2]])

b'\nlalaland\xff\x01\x02'

>>> format.parse(b"\x004361789432197")

['', [52, 51, 54, 49, 55, 56, 57, 52, 51, 50, 49, 57, 55]]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值