# Read HDF5 with variable number of keys

**URL:** <https://discourse.julialang.org/t/read-hdf5-with-variable-number-of-keys/35758>\
**Category:** General Usage\
**Tags:** question, hdf5\
**Created:** [March 9, 2020, 3:30pm UTC](https://discourse.julialang.org/t/read-hdf5-with-variable-number-of-keys/35758 "2020-03-09T15:30:02Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![met-j](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/met-j/32/21919_2.png) [@met-j](https://discourse.julialang.org/u/met-j)\
**Post date:** [March 9, 2020, 3:30pm UTC](https://discourse.julialang.org/t/read-hdf5-with-variable-number-of-keys/35758/1 "2020-03-09T15:30:02Z")

</div>

I get HDF5 files of a measurement. These files are organized like `[key_lvl1][key_lvl2][...]`.  
The structure in the deeper levels is always the same.  
However, the _first_ key level is a simple counter for the number of measurements stored in that specific file, e.g. `key_lvl1 = ["entry_1"]`, `["entry_2"]` and so on.  
Now I would like to read the whole file. For this I would have to specify the `string` of `key_lvl1` for all the entries - with out knowing it.

How can I read a HDF with out knowing all the `key_lvl1` `string`s in advance?

---

<div class="post-metadata">

**Author:** ![stillyslalom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stillyslalom/32/45687_2.png) [@stillyslalom](https://discourse.julialang.org/u/stillyslalom)\
**Post date:** [March 9, 2020, 7:00pm UTC](https://discourse.julialang.org/t/read-hdf5-with-variable-number-of-keys/35758/2 "2020-03-09T19:00:39Z")

</div>

If there aren’t any other entries in the first level, you can use

```julia
using HDF5
f = h5open(fname)
lvl1keys = names(f)

data = [f[joinpath(key, "rest/of/path")] for key in lvl1keys]
```

---

<div class="post-metadata">

**Author:** ![jhbdiehl](https://avatars.discourse-cdn.com/v4/letter/j/8491ac/32.png) [@jhbdiehl](https://discourse.julialang.org/u/jhbdiehl)\
**Post date:** [February 14, 2022, 8:03am UTC](https://discourse.julialang.org/t/read-hdf5-with-variable-number-of-keys/35758/3 "2022-02-14T08:03:30Z")

</div>

Note that names() has been renamed to keys, so the working example now would be

```julia
using HDF5
f = h5open(fname)
lvl1keys = keys(f)

data = [f[joinpath(key, "rest/of/path")] for key in lvl1keys]

```

---

<div class="post-metadata">

**Author:** ![met-j](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/met-j/32/21919_2.png) [@met-j](https://discourse.julialang.org/u/met-j)\
**Post date:** [February 14, 2022, 4:20pm UTC](https://discourse.julialang.org/t/read-hdf5-with-variable-number-of-keys/35758/4 "2022-02-14T16:20:49Z")

</div>

Thanks for the update!
