[英]Efficient translation to python of matlab fread
我需要將Matlab fread轉換為python,尤其是允許讀取到2d數組中並在讀取時跳過數據。 我提出了以下建議,但我想可能會有更高效和“ pythonic”的方法來做到這一點(我絕不是程序員)。 有什么建議嗎? 請注意,我無法讀取整個文件,然后對數組進行二次采樣,因為要讀取的文件太大。
def FromFileSkip(fid, count=1, skip=0, dtype=np.float32):
if np.ndim(count)==0:
if skip>=0:
data = np.zeros(count, dtype=dtype)
k = 0
while k<count:
data[k] = np.fromfile(fid, count=1, dtype=dtype)
fid.seek(skip, 1)
k +=1
return data
elif np.ndim(count)==1:
if skip>0:
data = np.zeros(count, dtype=dtype)
k = 0
while k<count[1]:
data[:,k] = np.fromfile(fid, count=count[0], dtype=dtype)
fid.seek(skip, 1)
k +=1
return data
else:
raise ValueError('File can be read only into 1d or 2d arrays')
這或多或少是您所擁有的,也許只是一點點清潔。
def fromfileskip(fid,shape,counts,skip,dtype):
"""
fid : file object, Should be open binary file.
shape : tuple of ints, This is the desired shape of each data block.
For a 2d array with xdim,ydim = 3000,2000 and xdim = fastest
dimension, then shape = (2000,3000).
counts : int, Number of times to read a data block.
skip : int, Number of bytes to skip between reads.
dtype : np.dtype object, Type of each binary element.
"""
data = np.zeros((counts,) + shape)
for c in xrange(counts):
block = np.fromfile(fid,dtype=np.float32,count=np.product(shape))
data[c] = block.reshape(shape)
fid.seek( fid.tell() + skip)
return data
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.