2
我想存儲一個數組,以在頭文件中添加一些額外的信息。我想使用numpy二進制'.npy'格式。我可以通過首先尋找數組部分的開頭來讀取帶有額外標頭的.npy文件的數組嗎?我可以通過使用seek向numpy的.npy文件添加額外的頭信息嗎?
我想做這樣的事情。如果一個標頭是'n'字節:
from tempfile import TemporaryFile
outfile = TemporaryFile()
# Write header to first 'n' bytes.
...
# Write the array after the header.
outfile.seek(n)
x = np.arange(10)
np.save(outfile, x)
# Then to read it back in:
outfile.seek(0)
# Read the header.
...
# Read the array.
outfile.seek(n)
y = np.load(outfile)