请教大家一个关于字典的小问题

a=[(1, Dict('A' => 0, 'G' => 1, 'T' => 0, 'C' => 0)),(7, Dict('A' => 3, 'G' => 1, 'T' => 0, 'C' => 2))]

2-element Vector{Tuple{Int64, Dict{Char, Int64}}}:
(1, Dict(‘A’ => 0, ‘G’ => 1, ‘T’ => 0, ‘C’ => 0))
(7, Dict(‘A’ => 3, ‘G’ => 1, ‘T’ => 0, ‘C’ => 2))

using DelimitedFiles
writedlm("hhh.dlm",a,'\t')
b=readdlm("hhh.dlm",'\t')

2×2 Matrix{Any}:
1 “Dict(‘A’ => 0, ‘G’ => 1, ‘T’ => 0, ‘C’ => 0)”
7 “Dict(‘A’ => 3, ‘G’ => 1, ‘T’ => 0, ‘C’ => 2)”

b[1,2]

“Dict(‘A’ => 0, ‘G’ => 1, ‘T’ => 0, ‘C’ => 0)”

我其实的本意是想让他返回一个字典的,然后根据key去取value,可是读进去输出为一个字符串了,各位大佬帮我看下我要在哪更改呢一下呢??

可能是DelimitedFiles这个包(话说没注册,给个链接?)不支持Dict的存储和读取而把它转化为字符串了

可以采用

  1. 联系DelimitedFiles开发者/阅读源码了解情况
  2. 使用eval将字符串转化
  3. 改用其它格式存储

第二种方法大概怎么弄呢?

julia> eval(Meta.parse("Dict('A' => 0, 'G' => 1, 'T' => 0, 'C' => 0)"))
Dict{Char, Int64} with 4 entries:
  'A' => 0
  'G' => 1
  'T' => 0
  'C' => 0

DelimitedFiles是用来存取文本文件的。如果你想存字典这类需要保持原本数据类型的,可以考虑其他的包,比如 JuliaIO/JLD2.jl: HDF5-compatible file format in pure Julia (github.com)

好的,已解决谢谢

备案号:京ICP备17009874号-2