0

我正在从给定的二进制格式读取数据,但是我只对字段的一个子集感兴趣。

例如:

MY_DTYPE = np.dtype({'names': ('A', 'B', 'C'), 'formats': ('<f8', '<u2', 'u1')})

data = np.fromfile(infile, count=-1, dtype=MY_DTYPE)

假设我真的不需要data['C'],是否可以首先指定我要保留的字段?

4

1 回答 1

1

模拟负载:

In [117]: MY_DTYPE = np.dtype({'names': ('A', 'B', 'C'), 'formats': ('<f8', '<u2', 'u1')})                   
In [118]: data = np.zeros(3, MY_DTYPE)                                                                       
In [119]: data                                                                                               
Out[119]: 
array([(0., 0, 0), (0., 0, 0), (0., 0, 0)],
      dtype=[('A', '<f8'), ('B', '<u2'), ('C', 'u1')])
In [120]: data['C']                                                                                          
Out[120]: array([0, 0, 0], dtype=uint8)

在最新的 numpy 版本中,多字段索引创建一个view

In [121]: data[['A','B']]                                                                                    
Out[121]: 
array([(0., 0), (0., 0), (0., 0)],
      dtype={'names':['A','B'], 'formats':['<f8','<u2'], 'offsets':[0,8], 'itemsize':11})

它提供了一个repack_fields功能来制作一个正确的copy

In [122]: import numpy.lib.recfunctions as rf                                                                
In [123]: rf.repack_fields(data[['A','B']])                                                                  
Out[123]: array([(0., 0), (0., 0), (0., 0)], dtype=[('A', '<f8'), ('B', '<u2')])

有关更多信息,请参阅文档repack,或查看最近的发行说明。

于 2019-09-10T01:46:23.240 回答