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

纵饮孤独 提交于 2020-02-01 03:22:22

问题


I have seen that Julia adecuately 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 writeddlm 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 unconfortable structure at each line

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

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


回答1:


You could use the JLD (Julia Data) submodule included in the HDF5 package:

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

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



来源:https://stackoverflow.com/questions/31013516/what-is-the-correct-way-to-save-and-retrieve-dictionaries-in-julia

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!