在 Julia 中保存和检索字典的正确方法是什么? [英] What is the correct way to save and retrieve dictionaries in Julia?
问题描述
我看到 Julia 充分解释了MAT";其中具有结构的文件可以毫无问题地作为字典读取.现在我已经创建了自己的字典,其结构如下
(String, String)=>[ 诠释,诠释,诠释]
在每个条目上.我可以用 writedlm
保存它,它会产生一个非常有序的表格文本文件,由制表符 (
) 分隔,但如果不进行大量解析,我将无法检索它.如果我使用 readdlm
我会得到一个类型为 Any
的数组,每行的结构都非常不舒服
"("Bla bla", "tururu")"[a, b, c]"
即两列String
,其中包含'"'
和'['
等符号.
您可以使用 HDF5
包:
Pkg.add("HDF5")使用 HDF5、JLDd = 字典(("a", "b") =>[1, 2, 3],("c", "d") =>[4, 5, 6],("e", "f") =>[7、8、9])保存(数据.jld",数据",d)加载(数据.jld")[数据"]
<块引用>
JLD 模块的优点是它保留了每个变量的准确类型信息.
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 (
), 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 String
s which contain signs such as '"'
and '['
.
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.
这篇关于在 Julia 中保存和检索字典的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!