Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python struct staticfield数据类型的新构造2.9语法_Python_Python 2.7_Construct - Fatal编程技术网

Python struct staticfield数据类型的新构造2.9语法

Python struct staticfield数据类型的新构造2.9语法,python,python-2.7,construct,Python,Python 2.7,Construct,我使用构造2.5.2实现了这一点: something=Struct("somename", Bytes("version",4), StaticField("somefieldname",32) ) myVar=something.parse('John') myVar.version=struct.pack('<I',1) myVar.somefieldname=struct.pack('<qqqq',0,0,0,0) 我用来存储新构造版本中调用的struct.pack结果的

我使用构造2.5.2实现了这一点:

something=Struct("somename",
Bytes("version",4),
StaticField("somefieldname",32)
)

myVar=something.parse('John')
myVar.version=struct.pack('<I',1)
myVar.somefieldname=struct.pack('<qqqq',0,0,0,0)

我用来存储新构造版本中调用的struct.pack结果的旧“StaticField”是什么?

StaticField
类在v中不再可用。2.9. 下面是使用
construct 2.9
语法的示例:

from construct import Int, Struct, GreedyString

# create struct
my_struct = Struct(
    "version" / Int,
    "somefieldname"/ GreedyString(encoding='utf-8')
)

# build an object in memory (a bytes object).
build = my_struct.build(dict(version=1, somefieldname="John"))

assert build == b'\x00\x00\x00\x01John'

# parse an in-memory buffer
parsed_data = my_struct.parse(build)

assert parsed_data.version == 1
assert parsed_data.somefieldname == "John"

有大量的示例介绍如何使用不同的数据类型。

感谢您的编辑,我现在为将来使用的样式:-)
from construct import Int, Struct, GreedyString

# create struct
my_struct = Struct(
    "version" / Int,
    "somefieldname"/ GreedyString(encoding='utf-8')
)

# build an object in memory (a bytes object).
build = my_struct.build(dict(version=1, somefieldname="John"))

assert build == b'\x00\x00\x00\x01John'

# parse an in-memory buffer
parsed_data = my_struct.parse(build)

assert parsed_data.version == 1
assert parsed_data.somefieldname == "John"