calcsize给出缓冲区将给定格式的字节数。在In [421]: struct.calcsize('>100h')
Out[421]: 200
In [422]: struct.calcsize('>100b')
Out[422]: 100
h每个项目占用2个字节,因此对于100个项目,它提供200个字节。在
对于frombuffer,第三个参数是
^{pr2}$
所以我应该给它100,而不是{}。在
读取一个简单的bytestring(在Py3中):In [429]: np.frombuffer(b'one two three ','b',14)
Out[429]: array([111, 110, 101, 32, 116, 119, 111, 32, 116, 104, 114, 101, 101, 32], dtype=int8)
In [430]: np.frombuffer(b'one two three ','h',14)
-
ValueError Traceback (most recent call last)
in ()
> 1 np.frombuffer(b'one two three ','h',14)
ValueError: buffer is smaller than requested size
In [431]: np.frombuffer(b'one two three ','h',7)
Out[431]: array([28271, 8293, 30580, 8303, 26740, 25970, 8293], dtype=int16)
要用h读它,我需要给它b读次数的一半。在