2017-09-06 87 views
0

我似乎无法得到此工作。所有的例子和线程都有人创建新的数据集。我只想更新已经创建的数据集中的一个字段。更新HDF中的一个字段文件

以下是我有:

h5_file = h5py.File(event_file_path, "r+") #this works 

event_processing_status = int(h5_file[PATH][STATUS].value[0]['Status']) #this works 
print({"{0:b}".format(event_processing_status)) #this works 
event_processing_status = (event_processing_status | STATUS_UPDATE) #this works 
h5_file[PATH][STATUS].value[0]['Status'] = event_processing_status #updating???, no error 
event_processing_status = int(h5_file[PATH][STATUS].value[0]['Status']) #this works 
print({"{0:b}".format(event_processing_status)) #not the update value 

h5_file.close() 

我在做什么错?

更多信息: 的数据集的列的dtypes:

dset = h5_file[PATH][STATUS] 
print(dset.dtype) gives: 
[('Status', '<u8'), ('Segments', '<u4'), ('Characterized', '<u4'), ('More_Segments', '<u4'), ('ID', '<i4'), ('Releases', '<u2'), ('Type', '|u1'), ('Track', '<i4')] 

dset[0,'Status'] = event_processing_status gives: 
TypeError: Field name selections are not allowed for write. 
+0

在这一行'h5_file [路径] [STATUS] .value [0] ['Status'])= event_processing_status'有一个额外的)赋值左边。它真的在那里吗?还是它是一个错字? – Moher

+0

复制和粘贴错字,对不起 – lr100

+0

尝试更改索引顺序,'[Status'] [0]'。我假设'Status'是一个结构化数组字段名称。 'h5py'lets您将记录号和字段名称合并为一个语句:'dset [0,'状态'] =状态'。 http://docs.h5py.org/en/latest/high/dataset.html#reading-writing-data。如果我的猜测没有帮助,请向我们展示此数据集的'dtype'。 – hpaulj

回答

0

这是我必须做的:

h5_file = h5py.File(event_file_path, "r+") #this works 

#Retrieve the dataset containing Event_Status 
data= h5_file[PATH][STATUS] 

#Get the Event_Status (np.array) 
event_status = data['Event_Status'] 

#Update the value of Event_Status 
event_status[0] = np.bitwise_or(event_status[0],np.uint64(STATUS_UPDATE)) 

#Write updated value to file 
elements = data[0] 
elements['Event_Status'] = event_status 
data[0] = elements 

h5_file.close() 
0

跟进我的评论,假设你的数据集是一种结构化/复合D型

In [144]: f = h5py.File('test.h5','w') 
In [145]: arr = np.ones((3,), dtype='i,f') # structured array 
In [146]: arr 
Out[146]: 
array([(1, 1.), (1, 1.), (1, 1.)], 
     dtype=[('f0', '<i4'), ('f1', '<f4')]) 

与创建数据集数据

In [147]: ds = f.create_dataset('arr',data=arr) 
In [148]: ds 
Out[148]: <HDF5 dataset "arr": shape (3,), type "|V8"> 
In [149]: ds.value 
Out[149]: 
array([(1, 1.), (1, 1.), (1, 1.)], 
     dtype=[('f0', '<i4'), ('f1', '<f4')]) 

我可以用记录ID和字段名称为它建立索引;这对ds.valuearr不起作用。

In [151]: ds[0,'f0'] 
Out[151]: 1 
In [152]: ds[0,'f0'] = 2 # and I can assign values 
In [153]: ds.value 
Out[153]: 
array([(2, 1.), (1, 1.), (1, 1.)], 
     dtype=[('f0', '<i4'), ('f1', '<f4')]) 

我可以与单独的记录和字段的条目索引;但不能改变的值是这样的:

In [154]: ds[0]['f1'] 
Out[154]: 1.0 
In [155]: ds[0]['f1'] = 234 
In [156]: ds.value 
Out[156]: 
array([(2, 1.), (1, 1.), (1, 1.)], 
     dtype=[('f0', '<i4'), ('f1', '<f4')]) 
In [157]: ds['f1'][0] = 234 
In [158]: ds.value 
Out[158]: 
array([(2, 1.), (1, 1.), (1, 1.)], 
     dtype=[('f0', '<i4'), ('f1', '<f4')]) 

分配要求的综合索引

In [159]: ds[0,'f1'] = 234 
In [160]: ds.value 
Out[160]: 
array([(2, 234.), (1, 1.), (1, 1.)], 
     dtype=[('f0', '<i4'), ('f1', '<f4')]) 
+0

尝试了你的作业定义,但它没有奏效。我更新了原始文章,更多的信息 – lr100

+0

@ lr100,你的'dset'dtype对我有用。你的'h5py'版本号是多少?我的是2.6.0 '。 – hpaulj

+0

我的h5py版本是2.0.0,无法更新 – lr100

相关问题