csv - 对于 Julia 将输出保存到 txt/CSV 文件?

标签 csv save output julia txt

我在 Win10 上使用 Julia 1.4.2 处理输出保存,我尝试了几种引用 docs 的方法。但对我来说没有任何用处。 我要保存的输出看起来像这样

julia> sim_result
GpABC.SimulatedABCRejectionOutput(3, 1465, 20000, 1.0, [4.5398045285832636 16.341298256602684 0.8528241692701137; 2.2014098882111117 11.88704704892733 1.8060482719350048; … ; 3.4569125171576727 14.206295598447817 1.2527525901911478; 1.1086538611233734 16.95205292134931 0.7814523160265465], [0.8718784407104312, 0.9897439825224637, 0.7361435718656393, 0.7863129137954922, 0.8097276891293583, 0.7167559341428585, 0.6600858843737456, 0.7420732257406872, 0.8085779131629843, 0.7603566788770011  …  0.9172504543539195, 0.5578993725435081, 0.8377191750525664, 0.7847440234430122, 0.6465487321194205, 0.716785821691151, 0.2596989086130145, 0.7210284576525666, 0.9855259563757373, 0.5775313512469311], [0.0006825938566552899, 0.0006825938566552899, 0.0006825938566552899, 0.0006825938566552899, 0.0006825938566552899, 0.0006825938566552899, 0.0006825938566552899, 0.0006825938566552899, 0.0006825938566552899, 0.0006825938566552899  …  0.0006825938566552899, 0.0006825938566552899, 0.0006825938566552899, 0.0006825938566552899, 0.0006825938566552899, 0.0006825938566552899, 0.0006825938566552899, 0.0006825938566552899, 0.0006825938566552899, 0.0006825938566552899])

它的结构是 GpABC.SimulatedABCRejectionOutput(::Int64,::Int64,::Int64,::Float64,::AbstractArray{Float64,2},::AbstractArray{Float64,1},::StatsBase.Weights) 我想将 ::AbstractArray{Float64,2} 保存到 TXT 或 CSV 文件。

最佳答案

很高兴提供这个作为实际答案 - 如果您在多个地方发帖,交叉引用总是好的,我在这里错过了这个。

第 1 步:了解如何访问数组 - 它将是 sim_result 对象的一个​​字段,您可以使用 fieldnames 来了解这些字段的访问方式这种类型的被称为。您需要使用 typeof(sim_result),因为 fieldnames 对类型进行操作。

julia> fieldnames(typeof(sim_result))
(:n_params, :n_accepted, :n_tries, :threshold, :population, :distances, :weights)

这告诉您可以作为 sim_result.population 访问数组。

第2步:如何将数组写入txt或csv?最简单的解决方案是使用 DelimitedFiles 标准库,它可以将分隔文件写入文本:

julia> using DelimitedFiles

julia> writedlm("output.txt", sim_result.population)

或者,如果你想写一个 csv 文件,你应该使用 CSV 包,它需要安装在你当前的环境中。然后你可以这样做:

julia> using CSV

julia> CSV.write("output.csv", (data = sim_result.population, ))

请注意,(data = sim_result.population, ) 中的逗号创建了一个 NamedTuple,这是 Julia 中最简单的表兼容对象(CSV 库需要一个表作为 CSV.write())

的参数

关于csv - 对于 Julia 将输出保存到 txt/CSV 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67172535/

相关文章:

javascript - 使用字符串创建变量

python - 保存工作簿时出现 KeyError

linux - 测试命令在 vbscript 中是否输出空字符串

Python - 使用 Matplotlib 绘制散点图时出错 : Index out of range

ios - 将内容从一个文件夹复制到另一个文件夹

mysql - CSV 到 MySQL 的转换和导入

python - 无法读取所需的 excel 文件作为 Python 中的输出

c++ - 仅清除部分控制台输出

javascript - 如何获取以逗号分隔的值到数组中?使用java脚本

javascript - Angular JS : Loading bar on CSV download from REST service