# How do I save my dictionary as a .csv (or excel) file?

**URL:** <https://discourse.julialang.org/t/how-do-i-save-my-dictionary-as-a-csv-or-excel-file/36791>\
**Category:** New to Julia\
**Created:** [March 31, 2020, 10:14am UTC](https://discourse.julialang.org/t/how-do-i-save-my-dictionary-as-a-csv-or-excel-file/36791 "2020-03-31T10:14:03Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![samantp](https://avatars.discourse-cdn.com/v4/letter/s/f475e1/32.png) [@samantp](https://discourse.julialang.org/u/samantp)\
**Post date:** [March 31, 2020, 10:14am UTC](https://discourse.julialang.org/t/how-do-i-save-my-dictionary-as-a-csv-or-excel-file/36791/1 "2020-03-31T10:14:03Z")

</div>

I have a dictionary, which I want to save to an excel or csv file with keys in one column and the entries in another. The keys are strings and the entries are float64s. I thought about converting my dictionary to a DataFrame but I am not sure if this type is supported in Julia or how to convert if it is, or even how to save a dataframe as csv. Can dictionaries be saved as csv files directly?

Thanks

---

<div class="post-metadata">

**Author:** ![TheLateKronos](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thelatekronos/32/12824_2.png) [@TheLateKronos](https://discourse.julialang.org/u/TheLateKronos)\
**Post date:** [March 31, 2020, 10:37am UTC](https://discourse.julialang.org/t/how-do-i-save-my-dictionary-as-a-csv-or-excel-file/36791/2 "2020-03-31T10:37:24Z")

</div>

This is easily done with the CSV package (If you don’t have it, run Pkg.add(“CSV”) in your REPL)

```julia
using CSV
test = Dict("a"=>1, "b"=>2, "c"=>3)
CSV.write("test.csv", test)

```

You can add a file-path like this: “/Users/your/custom/location/test.csv”

---

<div class="post-metadata">

**Author:** ![Iulian.Cioarca](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/iulian.cioarca/32/30166_2.png) [@Iulian.Cioarca](https://discourse.julialang.org/u/Iulian.Cioarca)\
**Post date:** [March 31, 2020, 10:46am UTC](https://discourse.julialang.org/t/how-do-i-save-my-dictionary-as-a-csv-or-excel-file/36791/3 "2020-03-31T10:46:46Z")

</div>

If you want to save in .xlsx:

```julia
using XLSX
x = Dict("a"=>1.0, "b" => 2.0)  
fid = XLSX.openxlsx("df.xlsx", mode="w")
header = ["keys";"values"]
data = [[collect(keys(x))];[collect(values(x))]]

XLSX.writetable("df.xlsx", data,header)

```

[https://felipenoris.github.io/XLSX.jl/stable/tutorial/](https://felipenoris.github.io/XLSX.jl/stable/tutorial/)

---

<div class="post-metadata">

**Author:** ![samantp](https://avatars.discourse-cdn.com/v4/letter/s/f475e1/32.png) [@samantp](https://discourse.julialang.org/u/samantp)\
**Post date:** [March 31, 2020, 10:58am UTC](https://discourse.julialang.org/t/how-do-i-save-my-dictionary-as-a-csv-or-excel-file/36791/4 "2020-03-31T10:58:00Z")

</div>

Thanks, that works. Is there a way to specify headers? I tried `CSV.write("File.csv",VariableName,["Column1Name";"Column2Name])` but I got a Method error.

---

<div class="post-metadata">

**Author:** ![samantp](https://avatars.discourse-cdn.com/v4/letter/s/f475e1/32.png) [@samantp](https://discourse.julialang.org/u/samantp)\
**Post date:** [March 31, 2020, 11:22am UTC](https://discourse.julialang.org/t/how-do-i-save-my-dictionary-as-a-csv-or-excel-file/36791/5 "2020-03-31T11:22:47Z")

</div>

Thanks! I think csv would be most convenient however. That being said the header argument you have specified doesn’t seem to work for csv.write ☹

---

<div class="post-metadata">

**Author:** ![Iulian.Cioarca](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/iulian.cioarca/32/30166_2.png) [@Iulian.Cioarca](https://discourse.julialang.org/u/Iulian.Cioarca)\
**Post date:** [March 31, 2020, 11:25am UTC](https://discourse.julialang.org/t/how-do-i-save-my-dictionary-as-a-csv-or-excel-file/36791/6 "2020-03-31T11:25:26Z")

</div>

The example is only for Excel files and it’s using the XLSX package.  
If you want to use CSV to write .csv files as in @TheLateKronos example, you could define your header as a comma delimited string, write that line to csv, and then write your `test` dictionary.

---

<div class="post-metadata">

**Author:** ![TheLateKronos](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thelatekronos/32/12824_2.png) [@TheLateKronos](https://discourse.julialang.org/u/TheLateKronos)\
**Post date:** [March 31, 2020, 11:31am UTC](https://discourse.julialang.org/t/how-do-i-save-my-dictionary-as-a-csv-or-excel-file/36791/7 "2020-03-31T11:31:21Z")

</div>

It seems you can specify custom column names when reading CSV’s, but I also just get the same error when I try to specify a vector of strings as column names. The best I can think of is to have the keyword argument append=true, which makes it so that column names are not written. Then you can add them yourself manually.

Alternatively find a way to write it to a dataframe with the column names you want, and save that

---

<div class="post-metadata">

**Author:** ![YiboXia](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yiboxia/32/50734_2.png) [@YiboXia](https://discourse.julialang.org/u/YiboXia)\
**Post date:** [September 18, 2023, 2:08am UTC](https://discourse.julialang.org/t/how-do-i-save-my-dictionary-as-a-csv-or-excel-file/36791/8 "2023-09-18T02:08:37Z")

</div>

Hello guys, still have a question:

how to read this .csv (or .xlsx) file into a variable with the type of Vector{Dict{}}?
