julia - 如何在 Julia 中读取记录格式 json?
问题描述
我可以使用以下代码读取 json 文件并转换为数据帧。
df = open(jsontable, "normal.json") |> DataFrame
normal.json
看起来像下面,
{"col1":["thasin", "hello", "world"],"col2":[1,2,3],"col3":["abc", "def", "ghi"]}
所以最终的df有,
3×3 DataFrame
│ Row │ col1 │ col2 │ col3 │
│ │ String │ Int64 │ String │
├─────┼────────┼───────┼────────┤
│ 1 │ thasin │ 1 │ abc │
│ 2 │ hello │ 2 │ def │
│ 3 │ world │ 3 │ ghi │
但是,相同的代码不适用于record
格式化的 json 文件。
格式是列表,如 {column -> value}, ... , {column -> value}
我的示例 json
{"billing_account_id":"0139A","credits":[],"invoice":{"month":"202003"},"cost_type":"regular"}
{"billing_account_id":"0139A","credits":[1.45],"invoice":{"month":"202003"},"cost_type":"regular"}
{"billing_account_id":"0139A","credits":[2.00, 3.56],"invoice":{"month":"202003"},"cost_type":"regular"}
预期输出:
billing_account_id cost_type credits invoice
0 0139A regular [] {'month': '202003'}
1 0139A regular [1.45] {'month': '202003'}
2 0139A regular [2.0, 3.56] {'month': '202003'}
这可以在 python 中完成,如下所示,
data = []
for line in open("sample.json", 'r'):
data.append(json.loads(line))
print(data)
df=pd.DataFrame(data)
如何在 Julia 中做到这一点?
解决方案
请注意,您的文件不是有效的 JSON(其行是有效的 JSON,而不是整个文件)。
你可以这样做:
julia> using DataFrames, JSON3
julia> df = JSON3.read.(eachline("sample.json")) |> DataFrame;
julia> df.credits = Vector{Float64}.(df.credits);
julia> df.invoice = Dict{Symbol,String}.(df.invoice);
julia> df
3×4 DataFrame
│ Row │ billing_account_id │ credits │ invoice │ cost_type │
│ │ String │ Array{Float64,1} │ Dict{Symbol,String} │ String │
├─────┼────────────────────┼────────────────────────────┼────────────────────────┼───────────┤
│ 1 │ 0139A │ 0-element Array{Float64,1} │ Dict(:month=>"202003") │ regular │
│ 2 │ 0139A │ [1.45] │ Dict(:month=>"202003") │ regular │
│ 3 │ 0139A │ [2.0, 3.56] │ Dict(:month=>"202003") │ regular │
:credits
和列的转换:invoice
是为了使它们的类型易于使用(否则它们使用由 JSON3.jl 内部定义的类型)。
更高级的选项是通过使用类型指定行模式来一次性完成,NamedTuple
例如:
julia> df = JSON3.read.(eachline("sample.json"),
NamedTuple{(:billing_account_id, :credits, :invoice, :cost_type),Tuple{String,Vector{Float64},Dict{String,String},String}}) |>
DataFrame
3×4 DataFrame
│ Row │ billing_account_id │ credits │ invoice │ cost_type │
│ │ String │ Array{Float64,1} │ Dict{String,String} │ String │
├─────┼────────────────────┼────────────────────────────┼─────────────────────────┼───────────┤
│ 1 │ 0139A │ 0-element Array{Float64,1} │ Dict("month"=>"202003") │ regular │
│ 2 │ 0139A │ [1.45] │ Dict("month"=>"202003") │ regular │
│ 3 │ 0139A │ [2.0, 3.56] │ Dict("month"=>"202003") │ regular │
推荐阅读
- java - 使用谓词集合过滤集合并符合阈值
- docker - 无法在 Jenkins 中构建 docker 映像
- php - 在 Woocommerce 3 中检查产品是否已在购物车中的条件函数
- java - Eclipse maven 项目仅在目标文件夹中创建 sources.jar 而不是 .jar
- css - 如何使用带有动画的css制作曲线?
- reactjs - 未捕获的 TypeError:使用 React 的“activedirectory”模块时的未知流类型“未定义”
- java - 如何在不使用 .equals 和错误布尔值的情况下比较数组
- db2 - dBeaver (CE):DB2 LUW 连接与 SQL 错误 42704。表架构将无法打开但能够编写 SQL 查询
- excel - 使用 VBA 根据特定值识别范围
- c - 如何通过配置文件 launch.json 在 Visual Studio 代码 (MacOS) 中调试 (.m) 文件?