# Reading hdf5 file saved with nested dict from Python

**URL:** <https://discourse.julialang.org/t/reading-hdf5-file-saved-with-nested-dict-from-python/90767>\
**Category:** General Usage\
**Tags:** hdf5, python\
**Created:** [November 24, 2022, 5:54pm UTC](https://discourse.julialang.org/t/reading-hdf5-file-saved-with-nested-dict-from-python/90767 "2022-11-24T17:54:53Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![rakshith95](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rakshith95/32/44606_2.png) [@rakshith95](https://discourse.julialang.org/u/rakshith95)\
**Post date:** [November 24, 2022, 5:54pm UTC](https://discourse.julialang.org/t/reading-hdf5-file-saved-with-nested-dict-from-python/90767/1 "2022-11-24T17:54:53Z")

</div>

Hello,  
I’m having problems reading hdf5 files using the ‘HDF5’ package, with nested dictionaries in them stored from Python. The data consists of a key (string) and its value (dict containing two keys for different arrays).

I saved it using deepdish , and I can read it fine in Python. However, when I read it in Julia using `matches = h5open(file)` , the data is like:

🗂 HDF5.File: (read-only) Photo\_SG\_matches.h5  
├─ 🏷 CLASS  
├─ 🏷 DEEPDISH\_IO\_DEEPDISH\_IO\_UNPACK  
├─ 🏷 DEEPDISH\_IO\_VERSION  
├─ 🏷 PYTABLES\_FORMAT\_VERSION  
├─ 🏷 TITLE  
├─ 🏷 VERSION  
└─ 🔢 data  
├─ 🏷 CLASS  
├─ 🏷 PSEUDOATOM  
├─ 🏷 TITLE  
└─ 🏷 VERSION

And if I run `read(matches, data)` it shows a 1-element vector of strings:

```julia
 1-element Vector{Vector{UInt8}}:
 [0x80, 0x04, 0x95, 0x43, 0x12, 0x01, 0x00, 0x00, 0x00, 0x00 … 0xd8, 0x23, 0x3f, 0x94, 0x74, 0x94, 0x62, 0x75, 0x75, 0x2e]

```

Am I missing something, or does Julia not support nested dicts?

---

<div class="post-metadata">

**Author:** ![fatteneder](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fatteneder/32/33991_2.png) [@fatteneder](https://discourse.julialang.org/u/fatteneder)\
**Post date:** [November 24, 2022, 7:14pm UTC](https://discourse.julialang.org/t/reading-hdf5-file-saved-with-nested-dict-from-python/90767/2 "2022-11-24T19:14:57Z")

</div>

Not an `HDF5` expert, but it looks like the Python package employs a custom serialization of Python types, probably because `HDF5` does not have a dictionary type.

Unless you find a better file format for which you know already that it can be also read with a package in Julia, you will have to check the Python library’s docs and figure how to make sense of their `data` field.

---

<div class="post-metadata">

**Author:** ![fatteneder](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fatteneder/32/33991_2.png) [@fatteneder](https://discourse.julialang.org/u/fatteneder)\
**Post date:** [November 24, 2022, 7:16pm UTC](https://discourse.julialang.org/t/reading-hdf5-file-saved-with-nested-dict-from-python/90767/3 "2022-11-24T19:16:31Z")

</div>

> [@rakshith95](#):
>
> Am I missing something, or does Julia not support nested dicts?

Julia supports nested `Dict`s:

```julia
julia> Dict(:a => Dict(:b => Dict(:c => "hello")))
Dict{Symbol, Dict{Symbol, Dict{Symbol, String}}} with 1 entry:
  :a => Dict(:b=>Dict(:c=>"hello"))

```

---

<div class="post-metadata">

**Author:** ![rakshith95](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rakshith95/32/44606_2.png) [@rakshith95](https://discourse.julialang.org/u/rakshith95)\
**Post date:** [November 25, 2022, 11:28am UTC](https://discourse.julialang.org/t/reading-hdf5-file-saved-with-nested-dict-from-python/90767/4 "2022-11-25T11:28:36Z")

</div>

Ah sorry, I meant to ask if the Julia HDF5 library supported nested dicts, not the language itself

---

<div class="post-metadata">

**Author:** ![fatteneder](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fatteneder/32/33991_2.png) [@fatteneder](https://discourse.julialang.org/u/fatteneder)\
**Post date:** [November 25, 2022, 1:50pm UTC](https://discourse.julialang.org/t/reading-hdf5-file-saved-with-nested-dict-from-python/90767/5 "2022-11-25T13:50:12Z")

</div>

According to the docs of `HDF5.jl` it does not: [Home · HDF5.jl](https://juliaio.github.io/HDF5.jl/stable/#Supported-data-types)

If you need a pkg to export Julia data types into `HDF5` format you could try `JLD2.jl`: [GitHub - JuliaIO/JLD2.jl: HDF5-compatible file format in pure Julia](https://github.com/JuliaIO/JLD2.jl)  
But again, this package employs a custom serialization because (I think) there is not dict type in the HDF5 specifications.  
The supported Julia types for export are explained here: [HDF5 Compatibility · Julia Data Format](https://juliaio.github.io/JLD2.jl/dev/hdf5compat/)

Either way, I think there is (not yet) a packaged solution that allows you to write nested dicts in Python and then import them in Julia, or vice versa.  
I guess you will have to write your own helper functions to get this done.

Also: If it is only about nested dicts that contain strings as values then your best bet might be to just use a `.json`.  
For python you could use the `json` module and for Julia you could use `JSON.jl`.

---

<div class="post-metadata">

**Author:** ![rakshith95](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rakshith95/32/44606_2.png) [@rakshith95](https://discourse.julialang.org/u/rakshith95)\
**Post date:** [November 25, 2022, 2:42pm UTC](https://discourse.julialang.org/t/reading-hdf5-file-saved-with-nested-dict-from-python/90767/6 "2022-11-25T14:42:17Z")

</div>

Thank you for the information. I guess for now, I will use PyCall and read in the hdf5 through the PyCall with the same library I used to save (deepdish). On trying that, it seems to work
