在 Julia 中将 DataFrame 输出到 csv 时,是否可以双引号所有字段?我无法通过 Google 找到答案。
在 python 中,我会将 quoting=csv.QUOTE_ALL
添加到 df.to_csv(file)
我找不到与 CSV.write(file,df)
类似的内容
最佳答案
您可以执行以下操作:
julia> using CSV, DataFrames
julia> io = IOBuffer()
IOBuffer(data=UInt8[...], readable=true, writable=true, seekable=true, append=false, size=0, maxsize=Inf,ptr=1, mark=-1)
julia> df = DataFrame(rand(1:10, 3, 5), :auto)
3×5 DataFrame
Row │ x1 x2 x3 x4 x5
│ Int64 Int64 Int64 Int64 Int64
─────┼───────────────────────────────────
1 │ 6 10 5 4 4
2 │ 1 9 6 5 3
3 │ 5 4 5 8 4
julia> CSV.write(io, df; quotestrings=true, transform=(col,val)->string(val)) |> take! |> String |> println
"x1","x2","x3","x4","x5"
"6","10","5","4","4"
"1","9","6","5","3"
"5","4","5","8","4"
问题是 quotestrings
只强制引用字符串(这样当你读回文件编号时,文件编号不会被引用和正确解析)因此你还需要 transform
参数强制每个值都写成字符串。
关于dataframe - 在 df 期间将所有内容引用到 julia 中的 csv,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72944802/