Writing a Dict to a CSV

Hi there,

I’d like to write a dictionary to a csv such that:

d1 = Dict(:a=>1, :b=>2, :c=>3)

writes to CSV as:

a b c
1 2 3

additionally I then want to append another dictionary as follows:

d2 = Dict(:a=>4, :b=>5, :c=>6)

CSV.write(“test.csv”, d2, append=true)

CSV would now read:

a b c
1 2 3
4 5 6

Struggling to find a way to acheive this. Any help would be appreciated. Thanks!

There might be a smarter way but I usually create a DataFrame from the dict and then write that to CSV.


The simplest, dependency-free approach is probably to just create a NamedTuple

julia> NamedTuple(d1)
(a = 1, b = 2, c = 3)

Which satisfies the Tables.Table interface and therefore can be written to CSV.