Return a string containing the values
v1, v2, ... packed according to the given format. The arguments must
match the values required by the format exactly.
--
struct.unpack(fmt, string)
Unpack the string (presumably packed by
pack(fmt, ...)) according to the given format. The result is a tuple
even if it contains exactly one item. The string must contain exactly
the amount of data required by the format (len(string) must equal
calcsize(fmt)).
请先看医生struct doc
--
因为
struct.pack
被定义为struct.pack(fmt, v1, v2, ...)
。 它接受一个非关键字参数列表(v1, v2, ...
,又名*args
), 所以struct.unpack
需要返回一个类似列表的对象,这就是tuple的原因。如果你认为背包是
示例:
考虑一个使用C语言编写的加载二进制数据的用例。如果二进制数据是使用结构或单个整数编写的,Python将无法区分。所以,我认为,逻辑上总是返回tuple是有意义的,因为struct pack和unpack在Python值和C结构之间执行转换。
相关问题 更多 >
编程相关推荐