二进制文件转npy文件(Python3)
June 17, 2019, 7:36 p.m.
read: 1408
以下demo只支持float格式,仅提供思路,其他数据类型需自行扩展:
import numpy as np
import struct
def bin2npy(bin_file_path, npy_file_path, type='float'):
assert type in ['float']
out_data = []
with open(bin_file_path, mode='rb') as bin_file:
in_data = bin_file.read()
for i in range(len(in_data)//4):
out_data.append(struct.unpack("f", in_data[i*4:(i+1)*4]))
# return np.array(out_data)
np.save(npy_file_path, np.array(out_data))
if __name__ == '__main__':
bin2npy('/Users/hupeng/work/60/lstm_in_63.bin', '/Users/hupeng/work/60/lstm_in_63.npy')