numpy.frombuffer
numpy.frombuffer(buffer, dtype=float, count=-1, offset=0)
将缓冲区解释为一维数组。
参数: | buffer : 公开缓冲区接口的对象。 dtype : 返回array的数据类型;默认值:float。 count : 要阅读的条目数。 offset : 从这个偏移量(以字节为单位)开始读取缓冲区;默认值: |
Notes
如果缓冲区中的数据不是机器字节顺序的,则应将其指定为数据类型的一部分,例如:
>>> dt = np.dtype(int)>>> dt = dt.newbyteorder('>')>>> np.frombuffer(buf, dtype=dt)
结果数组的数据将不会被字节包装,但是将被正确地解释
例子
>>> s = b'hello world'>>> np.frombuffer(s, dtype='S1', count=5, offset=6)array([b'w', b'o', b'r', b'l', b'd'], dtype='|S1')
>>> np.frombuffer(b'\x01\x02', dtype=np.uint8)array([1, 2], dtype=uint8)>>> np.frombuffer(b'\x01\x02\x03\x04\x05', dtype=np.uint8, count=3)array([1, 2, 3], dtype=uint8)