numpy.frombuffer()

  • 2019 年 10 月 28 日
  • 筆記

版權聲明:本文為博主原創文章,遵循 CC 4.0 BY-SA 版權協議,轉載請附上原文出處鏈接和本聲明。

本文鏈接:https://blog.csdn.net/weixin_36670529/article/details/102668346

numpy.frombuffer

numpy.frombuffer(bufferdtype=floatcount=-1offset=0)

Interpret a buffer as a 1-dimensional array.

Parameters:

buffer : buffer_like An object that exposes the buffer interface. dtype : data-type, optional Data-type of the returned array; default: float. count : int, optional Number of items to read. -1 means all data in the buffer. offset : int, optional Start reading the buffer from this offset (in bytes); default: 0.

Notes

If the buffer has data that is not in machine byte-order, this should be specified as part of the data-type, e.g.:

>>> dt = np.dtype(int)  >>> dt = dt.newbyteorder(『>『)  >>> np.frombuffer(buf, dtype=dt)

The data of the resulting array will not be byteswapped, but will be interpreted correctly.

Examples

>>> s = 『hello world『  >>> np.frombuffer(s, dtype=『S1『, count=5, offset=6)  array([『w『, 『o『, 『r『, 『l『, 『d『],        dtype=『|S1『)
>>> np.frombuffer(b『x01x02『, dtype=np.uint8)  array([1, 2], dtype=uint8)  >>> np.frombuffer(b『x01x02x03x04x05『, dtype=np.uint8, count=3)  array([1, 2, 3], dtype=uint8)

NumPy的ndarray數組對象不能像list一樣動態地改變其大小,在做數據採集時很不方便。本文介紹如何通過np.frombuffer()實現動態數組。