fileparsingjulia

What is the correct way to save and retrieve dictionaries in Julia?


I have seen that Julia adequately interprets "MAT" files which have structures in them which are read as dictionaries without problem. Now I have created a dictionary of my own, which has the following structure

(String, String)=> [ Int, Int, Int]

on each entry. I can save it with writedlm and it produces a very orderly tabular text file, separated by tabs (\t), but then I cannot retrieve it without doing a LOT of parsing. If I use readdlm I get an array of type Any, with the very uncomfortable structure at each line

"(\"Bla bla\", \"tururu\")"     "[a, b, c]"

That is, two columns of Strings which contain signs such as '"' and '['.


Solution

  • You could use the JLD2 (Julia Data Version 2) package:

    Pkg.add("JLD2")
    using JLD2
    d = Dict(
        ("a", "b") => [1, 2, 3],
        ("c", "d") => [4, 5, 6],
        ("e", "f") => [7, 8, 9]
    )
    save("data.jld2", "data", d)
    load("data.jld2")["data"]
    

    the advantage of the JLD2 module is that it preserves the exact type information of each variable.