numpy.frombuffer()详细介绍
frombuffer将data以流的形式读入转化成ndarray对象 numpy.frombuffer(buffer, dtype=float, count=-1, offset=0) buffer:缓冲区,它表示暴露缓冲区接口的对象。 dtype:代表返回的数据类型数组的数据类型。默认值为0。 count:代表返回的ndarray的长度。默认值为-1。 offset:偏移量,代表读取的起始位置。默认值为0。
#data是字符串的时候,Python3默认str是Unicode类型,所以要转成bytestring在原str前加上b import numpy as np data =bhello world! res = np.frombuffer(data,dtype=S3,offset=0) print(res)
输出结果:
[bhel blo bwor bld!]
# 读文件
# gzip文件读写的时候需要用到Python的gzip模块。具体使用如下:
import numpy as np
with gzip.open(filepath,rb)as f :
data = np.frombuffer(f.read(),np.uint8,offset=8)
ndarray.reshape()函数里面的参数-1是表示: 模糊控制,不知道要转换后-1的位置有多少个,反正其他的就按照指定的来
