您可以將打開的文件對象傳遞到np.fromfile
,讀取第一個數組的維度,然後讀取數組內容(再次使用np.fromfile
),併爲同一文件中的其他數組重複此過程。
例如:
import numpy as np
import os
def iter_arrays(fname, array_ndim=2, dim_dtype=np.int, array_dtype=np.double):
with open(fname, 'rb') as f:
fsize = os.fstat(f.fileno()).st_size
# while we haven't yet reached the end of the file...
while f.tell() < fsize:
# get the dimensions for this array
dims = np.fromfile(f, dim_dtype, array_ndim)
# get the array contents
yield np.fromfile(f, array_dtype, np.prod(dims)).reshape(dims)
用法示例:
# write some random arrays to an example binary file
x = np.random.randn(100, 200)
y = np.random.randn(300, 400)
with open('/tmp/testbin', 'wb') as f:
np.array(x.shape).tofile(f)
x.tofile(f)
np.array(y.shape).tofile(f)
y.tofile(f)
# read the contents back
x1, y1 = iter_arrays('/tmp/testbin')
# check that they match the input arrays
assert np.allclose(x, x1) and np.allclose(y, y1)
如果陣列是大的,你可以考慮使用np.memmap
與地方np.fromfile
的offset=
參數,以獲得數組的內容作爲內存映射而不是將它們加載到RAM中。
你能描述一下文件的格式嗎?在不瞭解文件本身的情況下很難推薦一種特定的方法。 –
它是一個原始的二進制文件,它包含來自C++程序的雙精度矩陣,以及描述矩陣大小的整數 – jacob
單個文件是否包含多個數組,或者每個文件只有一個數組?數組的大小是否在文件開頭的標題中給出?你能描述標題嗎? –