numpy.ndarray.tobytes#
方法
- ndarray.tobytes(order='C')#
构建包含数组中原始数据字节的 Python 字节。
构建 Python 字节,其中包含数据内存原始内容的副本。默认情况下,字节对象以 C-order 方式生成。此行为由
order
参数控制。- 参数:
- order{‘C’, ‘F’, ‘A’},可选
控制字节对象的内存布局。‘C’ 表示 C-order,‘F’ 表示 F-order,‘A’(Any 的缩写)表示如果 a 是 Fortran 连续的,则为 ‘F’,否则为 ‘C’。默认值为 ‘C’。
- 返回:
- sbytes
表示 a 原始数据副本的 Python 字节。
另请参阅
frombuffer
此操作的逆向,从 Python 字节构建一维数组。
示例
>>> import numpy as np >>> x = np.array([[0, 1], [2, 3]], dtype='<u2') >>> x.tobytes() b'\x00\x00\x01\x00\x02\x00\x03\x00' >>> x.tobytes('C') == x.tobytes() True >>> x.tobytes('F') b'\x00\x00\x02\x00\x01\x00\x03\x00'