# Exporting/printing an array of dictionaries to external text file

**URL:** <https://discourse.julialang.org/t/exporting-printing-an-array-of-dictionaries-to-external-text-file/117340>\
**Category:** New to Julia\
**Tags:** question, array, io\
**Created:** [July 22, 2024, 12:39pm UTC](https://discourse.julialang.org/t/exporting-printing-an-array-of-dictionaries-to-external-text-file/117340 "2024-07-22T12:39:37Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![shrdlu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shrdlu/32/210822_2.png) [@shrdlu](https://discourse.julialang.org/u/shrdlu)\
**Post date:** [July 22, 2024, 12:39pm UTC](https://discourse.julialang.org/t/exporting-printing-an-array-of-dictionaries-to-external-text-file/117340/1 "2024-07-22T12:39:37Z")

</div>

Hi,

I’m trying to export a Julia array (of dictionaries, among some oddities) to a text file to share with a colleague.

The array in question looks something like this, but about 70k entries:

```julia
 Dict{String, Any}("tem" => false, "cl" => "B")
 nothing
 Dict{String, Any}("tem" => true, "cl" => "AS")
 Dict{String, Any}("tem" => false, "cl" => "CR")
 Dict{String, Any}("tem" => false, "cl" => "EB")

```

So far I tried following the 2018 solution posted on a similar topic:

```julia
f = open("demo.txt", "w")

for i in eachindex(slice_array)
println(f, slice_array[i])
end

```

And a to-the-letter version:

```julia
outfile="demo.txt"
f = open(outfile,"w")

for i in eachindex(slice_array)
println(f, slice_array[i])
end

```

While both approaches do create a ‘demo.txt’ output file, the content of the file itself is empty. Testing the loop portion without the f io does seem to print all the lines in the REPL without issues.

What could I be doing wrong?

Any advice would be appreciated. Thank you!

---

<div class="post-metadata">

**Author:** ![screw\_dog](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/screw_dog/32/48119_2.png) [@screw\_dog](https://discourse.julialang.org/u/screw_dog)\
**Post date:** [July 22, 2024, 12:42pm UTC](https://discourse.julialang.org/t/exporting-printing-an-array-of-dictionaries-to-external-text-file/117340/2 "2024-07-22T12:42:47Z")

</div>

Looks like you just need to `close` the file after the writes.

It’s usually preferred to use the `do` block form of the `open` function which ensures this will happen automatically:

```julia
outfile="demo.txt"
open(outfile, "w") do f
  for i in eachindex(slice_array)
    println(f, slice_array[i])
  end
end

```

---

<div class="post-metadata">

**Author:** ![shrdlu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shrdlu/32/210822_2.png) [@shrdlu](https://discourse.julialang.org/u/shrdlu)\
**Post date:** [July 22, 2024, 12:45pm UTC](https://discourse.julialang.org/t/exporting-printing-an-array-of-dictionaries-to-external-text-file/117340/3 "2024-07-22T12:45:00Z")

</div>

Yes, that fixed everything right up - now I feel silly.

Thank you for the help!

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [July 22, 2024, 1:21pm UTC](https://discourse.julialang.org/t/exporting-printing-an-array-of-dictionaries-to-external-text-file/117340/4 "2024-07-22T13:21:18Z")

</div>

I would really consider using JSON here.

- JSON.jl
- JSON3.jl

---

<div class="post-metadata">

**Author:** ![shrdlu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shrdlu/32/210822_2.png) [@shrdlu](https://discourse.julialang.org/u/shrdlu)\
**Post date:** [July 22, 2024, 9:04pm UTC](https://discourse.julialang.org/t/exporting-printing-an-array-of-dictionaries-to-external-text-file/117340/5 "2024-07-22T21:04:48Z")

</div>

Thank you for the suggestion!

The original entry is produced using JSON.jl - I’m trying to make one mess of a file (compiled by unrelated undergrads from across dozens of unis) with lots of sub-sub-branches and occasional non-formatted personal comments & gibberish I don’t understand thrown in for a good measure.

I do have additional questions on piecing this thing together, but will write them on a separate post.
