Here is a solution based on numpy's implementaion of save
to write a standard npy
file including shape and type information:
import numpy as np
import numpy.lib as npl
a = np.random.random((30, 3, 2))
a1 = a[:10]
a2 = a[10:]
filename = 'out.npy'
with open(filename, 'wb+') as f:
header = npl.format.header_data_from_array_1_0(a1)
npl.format.write_array_header_1_0(f, header)
a1.tofile(f)
a2.tofile(f)
f.seek(0)
header['shape'] = (len(a1) + len(a2), *header['shape'][1:])
npl.format.write_array_header_1_0(f, header)
assert (np.load(filename) == a).all()
This works for C_CONTIGUOUS
arrays without Python objects.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…