h5py 无法打开用 h5py 创建的 HDF5 文件 [英] HDF5 file created with h5py can't be opened by h5py

查看:82
本文介绍了h5py 无法打开用 h5py 创建的 HDF5 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Ubuntu 12.04(32 位版本)下创建了一个 HDF5 文件,显然没有任何问题,使用 Anaconda 作为 Python 发行版并在 ipython 笔记本中编写.底层数据都是numpy数组.例如,

I created an HDF5 file apparently without any problems, under Ubuntu 12.04 (32bit version), using Anaconda as Python distribution and writing in ipython notebooks. The underlying data are all numpy arrays. For example,

import numpy as np
import h5py

f = h5py.File('myfile.hdf5','w')

group = f.create_group('a_group')

group.create_dataset(name='matrix', data=np.zeros((10, 10)), chunks=True, compression='gzip')

但是,如果我尝试从新的 iypthon 笔记本打开此文件,则会收到一条错误消息:

If I try to open this file from a new iypthon notebook, though, I get an error message:

f = h5py.File('myfile.hdf5', "r")

---------------------------------------------------------------------------
IOError                                   Traceback (most recent call last)
<ipython-input-4-b64ac5089cd4> in <module>()
----> 1 f = h5py.File(file_name, "r")

/home/sarah/anaconda/lib/python2.7/site-packages/h5py/_hl/files.pyc in __init__(self, name, mode, driver, libver, userblock_size, **kwds)
    220 
    221             fapl = make_fapl(driver, libver, **kwds)
--> 222             fid = make_fid(name, mode, userblock_size, fapl)
    223 
    224         Group.__init__(self, fid)

/home/sarah/anaconda/lib/python2.7/site-packages/h5py/_hl/files.pyc in make_fid(name, mode, userblock_size, fapl, fcpl)
     77 
     78     if mode == 'r':
---> 79         fid = h5f.open(name, h5f.ACC_RDONLY, fapl=fapl)
     80     elif mode == 'r+':
     81         fid = h5f.open(name, h5f.ACC_RDWR, fapl=fapl)

/home/sarah/anaconda/lib/python2.7/site-packages/h5py/h5f.so in h5py.h5f.open (h5py/h5f.c:1741)()

IOError: Unable to open file (Unable to find a valid file signature)

你能告诉我丢失的文件签名是什么吗?我创建文件时是否遗漏了什么?

Can you tell me what that missing file signature is? Did I miss something when I created the file?

推荐答案

由于我们在对我的问题的评论中解决了该问题,因此我将结果写在此处以将其标记为已解决.

Since we resolved the issue in the comments on my question, I'm writing the results out here to mark it as solved.

主要问题是我在创建文件后忘记关闭它.有两个简单的选择,要么:

The main problem was that I forgot to close the file after I created it. There would have been two simple options, either:

import numpy as np
import h5py

f = h5py.File('myfile.hdf5','w')
group = f.create_group('a_group')
group.create_dataset(name='matrix', data=np.zeros((10, 10)), chunks=True, compression='gzip')
f.close()

或者,我最喜欢的,因为文件会自动关闭:

or, my favourite because the file is closed automatically:

import numpy as np
import h5py

with h5py.File('myfile.hdf5','w') as f:
    group = f.create_group('a_group')
    group.create_dataset(name='matrix', data=np.zeros((10, 10)), chunks=True, compression='gzip')

这篇关于h5py 无法打开用 h5py 创建的 HDF5 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆