Python struct.pack() for individual elements in a list?

寵の児 提交于 2019-12-05 00:01:17

Yes, you can use the *args calling syntax.

Instead of this:

buf = struct.pack('d'*NumElements,data)  # Returns error

… do this:

buf = struct.pack('d'*NumElements, *data) # Works

See Unpacking Argument Lists in the tutorial. (But really, read all of section 4.7, not just 4.7.4, or you won't know what "The reverse situation…" is referring to…) Briefly:

… when the arguments are already in a list or tuple but need to be unpacked for a function call requiring separate positional arguments… write the function call with the *-operator to unpack the arguments out of a list or tuple…

The format string for struct.pack(...) and struct.unpack(...) allows to pass number(representing count) in front of type, with meaning how many times is the specific type expected to be present in the serialised data:

Simple case

data = [1.2, 3.4, 5.6]
struct.pack('3d', data[0], data[1], data[2])
struct.pack('3d', *[1.2, 3.4, 5.6])

or more generally:

data = [1.0, 1.234, 1.9, 3.14, 6.002, 7.4, 9.2]
struct.pack('{}d'.format(len(data)), *data)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!