# DelimitedFiles:writedlm usage

**URL:** <https://discourse.julialang.org/t/delimitedfiles-writedlm-usage/33009>\
**Category:** General Usage\
**Created:** [January 5, 2020, 11:22pm UTC](https://discourse.julialang.org/t/delimitedfiles-writedlm-usage/33009 "2020-01-05T23:22:45Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![tanya](https://avatars.discourse-cdn.com/v4/letter/t/35a633/32.png) [@tanya](https://discourse.julialang.org/u/tanya)\
**Post date:** [January 5, 2020, 11:22pm UTC](https://discourse.julialang.org/t/delimitedfiles-writedlm-usage/33009/1 "2020-01-05T23:22:45Z")

</div>

with using DelimitedFiles  
What would be the equivalent of matlab command dlmwrite(‘filename.csv’,arrayname,‘delimiter’,‘,’,‘-append’) ?  
Basically, I need to write to the same csv file and append the existing data.

writedlm(“filename.csv”,arrayname,‘,’) writes to an csv file, but it does not append. The help mentions some “opns” presumably options but i could not find a single example. I tried to see csv.jl package, but it seems equally lacking any documentation.

Many thanks

---

<div class="post-metadata">

**Author:** ![affans](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/affans/32/11911_2.png) [@affans](https://discourse.julialang.org/u/affans)\
**Post date:** [January 5, 2020, 11:24pm UTC](https://discourse.julialang.org/t/delimitedfiles-writedlm-usage/33009/2 "2020-01-05T23:24:44Z")

</div>

You should look into the `CSV.jl` package. Example usage would be `CSV.write("filename.csv", arrayname, delim=",")`

---

<div class="post-metadata">

**Author:** ![tanya](https://avatars.discourse-cdn.com/v4/letter/t/35a633/32.png) [@tanya](https://discourse.julialang.org/u/tanya)\
**Post date:** [January 5, 2020, 11:45pm UTC](https://discourse.julialang.org/t/delimitedfiles-writedlm-usage/33009/3 "2020-01-05T23:45:52Z")

</div>

But will it APPEND? I looked at it - yes it says it appends, but it requires data to be in some special form.

All I want is just to write an array of floats , say randn(3,5) into an csv file several times.

I would have thought that it is a reasonable request that a language should allow to do this without any bells and whistles, data conversions etc… Is there any package which can write such an array into a file and append?

---

<div class="post-metadata">

**Author:** ![affans](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/affans/32/11911_2.png) [@affans](https://discourse.julialang.org/u/affans)\
**Post date:** [January 5, 2020, 11:57pm UTC](https://discourse.julialang.org/t/delimitedfiles-writedlm-usage/33009/4 "2020-01-05T23:57:38Z")

</div>

Yes. There is also an `append` keyword argument. See documentation here: [Home · CSV.jl](https://juliadata.github.io/CSV.jl/stable/#CSV.write)

`CSV.write("filename.csv", arrayname, delim=",", append=true)`

Edit: I see you are trying to write a vector to the file. I am pretty sure `CSV.jl` expects the data in a table format. The easiest way would be to simply convert it to `DataFrame`.

```julia
a = randn(5, 3) 
CSV.write("filename.csv", DataFrame(a), delim=",", append=true)

```

---

<div class="post-metadata">

**Author:** ![tanya](https://avatars.discourse-cdn.com/v4/letter/t/35a633/32.png) [@tanya](https://discourse.julialang.org/u/tanya)\
**Post date:** [January 5, 2020, 11:58pm UTC](https://discourse.julialang.org/t/delimitedfiles-writedlm-usage/33009/5 "2020-01-05T23:58:26Z")

</div>

Data conversion? say I have array = randn(5,3)

It refuses to do anything with this array.

---

<div class="post-metadata">

**Author:** ![affans](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/affans/32/11911_2.png) [@affans](https://discourse.julialang.org/u/affans)\
**Post date:** [January 6, 2020, 12:08am UTC](https://discourse.julialang.org/t/delimitedfiles-writedlm-usage/33009/6 "2020-01-06T00:08:44Z")

</div>

Convert to a dataframe. There are other IO packages that may work directly with an array, but maybe someone else can chime in on that.

```julia
a = randn(5, 3) 
CSV.write("filename.csv", DataFrame(a), delim=",", append=true)

```

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [January 6, 2020, 12:13am UTC](https://discourse.julialang.org/t/delimitedfiles-writedlm-usage/33009/7 "2020-01-06T00:13:55Z")

</div>

Look at [Delimited Files · The Julia Language](https://docs.julialang.org/en/v1/stdlib/DelimitedFiles/)

I’m guessing that you should replace the “w” (write) with “a” (append). (Not tested)

---

<div class="post-metadata">

**Author:** ![tanya](https://avatars.discourse-cdn.com/v4/letter/t/35a633/32.png) [@tanya](https://discourse.julialang.org/u/tanya)\
**Post date:** [January 6, 2020, 12:15am UTC](https://discourse.julialang.org/t/delimitedfiles-writedlm-usage/33009/8 "2020-01-06T00:15:37Z")

</div>

Thanks for this - done conversion and it works!

I did not mean to be grumpy, and most grateful, it is just the whole thing does not stop to surprise: key functions are delegated to obscure packages, main packages do not have obvious things, many things are counter intuitive… Who would have thought that you need to install like three packages and write a lot of idiotic text to print a float variable to a file…

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [January 6, 2020, 1:30am UTC](https://discourse.julialang.org/t/delimitedfiles-writedlm-usage/33009/9 "2020-01-06T01:30:35Z")

</div>

You can also rethink what you are trying to do. Why would you choose a CSV format for this task with small arrays? You are coming from another language with a limited view of the alternatives. Try to ask the more general question: I am trying to do X, and so far I considered implementing it with Y. People here will likely say: X is better implemented with method Z instead.

In your use case, my guess is that there are many different formats implemented in packages that are more appropriate than CSV for appending small arrays to a file. Try to unlearn bad programming habits you learned in other programming environments, and keep in mind that many people are here to help with the transition.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [January 6, 2020, 7:40am UTC](https://discourse.julialang.org/t/delimitedfiles-writedlm-usage/33009/10 "2020-01-06T07:40:23Z")

</div>

> [@tanya](#):
>
> key functions are delegated to obscure packages

Writing delimited files is hardly a core feature (compared to, say, `+`), and [`DelimitedFiles`](https://docs.julialang.org/en/v1/stdlib/DelimitedFiles/) is not obscure at all — it is part of the standard libraries.

> [@tanya](#):
>
> Who would have thought that you need to install like three packages and write a lot of idiotic text to print a float variable to a file…

You only need DelimitedFiles, which you do not need to install (not that there would be a problem with that, since it is really easy to install packages), and the solution is a one-liner.

---

<div class="post-metadata">

**Author:** ![tanya](https://avatars.discourse-cdn.com/v4/letter/t/35a633/32.png) [@tanya](https://discourse.julialang.org/u/tanya)\
**Post date:** [January 6, 2020, 9:52am UTC](https://discourse.julialang.org/t/delimitedfiles-writedlm-usage/33009/11 "2020-01-06T09:52:35Z")

</div>

Hi Juliohm

I actually have files growing to about 5GB in csv form, and this is why I append relatively small chunks (1000 lines) to it at at every step of a loop. May be other formats work better with this, but I already have a collection of files like this.

I agree that in the best possible world one needs to learn new language as a new langage to appreciate its features, and I wish I could do this, but many of us need something done quickly, do no have time for any reading, unfortunately, and go for julia as it speeds up noticeably (in my experience), so here is: let me rewrite the existing code somehow so that it works and it works faster, and I will sort out nice things later on…

hence the absence of documentation when you search for a very particular thing is frustrating and the forum is the only option.

---

<div class="post-metadata">

**Author:** ![tanya](https://avatars.discourse-cdn.com/v4/letter/t/35a633/32.png) [@tanya](https://discourse.julialang.org/u/tanya)\
**Post date:** [January 6, 2020, 10:01am UTC](https://discourse.julialang.org/t/delimitedfiles-writedlm-usage/33009/12 "2020-01-06T10:01:36Z")

</div>

Hi Tamas\_Papp

If I have an opportunity to ask, which file format is the core feature in Julia, so that it will help me to write an appendable file, then read from it from a particular line? If not csv, then what? There must be something like this…

---

<div class="post-metadata">

**Author:** ![daniel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/daniel/32/6480_2.png) [@daniel](https://discourse.julialang.org/u/daniel)\
**Post date:** [January 6, 2020, 11:27am UTC](https://discourse.julialang.org/t/delimitedfiles-writedlm-usage/33009/13 "2020-01-06T11:27:35Z")

</div>

There is no core feature file format in Julia, even though I don’t know what that’s supposed to mean. The Julia devs are no overlords who decide what is and is not supported. The Julia code that you write works exactly like the Julia code in Base or the standard library.

If you want to append something to a csv file, you can either use DelimitedFiles or CSV.jl.  
For DelimitedFiles, you could use “a” instead of “w” in `open`:

```julia
using DelimitedFiles

open("delimited_file.csv", "a") do io
    for i in 1:4
        writedlm(io, [i i i], ',')
    end
end

```

---

<div class="post-metadata">

**Author:** ![tanya](https://avatars.discourse-cdn.com/v4/letter/t/35a633/32.png) [@tanya](https://discourse.julialang.org/u/tanya)\
**Post date:** [January 6, 2020, 11:51am UTC](https://discourse.julialang.org/t/delimitedfiles-writedlm-usage/33009/14 "2020-01-06T11:51:12Z")

</div>

Daniel  
Thanks a lot, this should work: the append command is given on the level of “open” not “write”.  
just for the record, it seems to me that, csv.write with this transformatio to datatables works quite slow. But this is naked eye observation

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [January 6, 2020, 11:59am UTC](https://discourse.julialang.org/t/delimitedfiles-writedlm-usage/33009/15 "2020-01-06T11:59:13Z")

</div>

To second @daniel, the no specific file format is treated specially in Julia: support is mostly in packages.

Unless you have an overwhelming need for something like HDF5 (which also has a package), I would go with CSV.

Also, while _parsing_ CSV can be tricky, _writing_ it should be fast, especially with `DelimitedFiles`. Here is an example:

```julia
using DelimitedFiles
A = reshape(1:4, 2, :) # we write this twice
open(io -> writedlm(io, A, ','), "/tmp/test.csv", "w") # write
open(io -> writedlm(io, A, ','), "/tmp/test.csv", "a") # append

```

---

<div class="post-metadata">

**Author:** ![daniel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/daniel/32/6480_2.png) [@daniel](https://discourse.julialang.org/u/daniel)\
**Post date:** [January 6, 2020, 12:00pm UTC](https://discourse.julialang.org/t/delimitedfiles-writedlm-usage/33009/16 "2020-01-06T12:00:02Z")

</div>

This is probably compilation time you’re noticing. CSV.jl speed is pretty competitive, see [Benchmarks: CSV readers | Queryverse](https://www.queryverse.org/benchmarks/csvreaders/) (the link says please don’t cite yet but I’m a rebel). It may be the case though that you run into some weird corner case with your data.

---

<div class="post-metadata">

**Author:** ![tanya](https://avatars.discourse-cdn.com/v4/letter/t/35a633/32.png) [@tanya](https://discourse.julialang.org/u/tanya)\
**Post date:** [January 6, 2020, 9:44pm UTC](https://discourse.julialang.org/t/delimitedfiles-writedlm-usage/33009/17 "2020-01-06T21:44:24Z")

</div>

May be 🙂 I would not insist, but adding one package instead of two (csv + dataframes) is always better!

Anyway, many thanks, I made it working, and direct “translation” of a MATLAB code into Julia code gave me gain 2.5 times! So instead of 5 weeks it will only run 2!

I suspect “writing in Julia” rather than “translate” would do miracles.

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [January 6, 2020, 9:51pm UTC](https://discourse.julialang.org/t/delimitedfiles-writedlm-usage/33009/18 "2020-01-06T21:51:28Z")

</div>

If you do some profiling to find the part that’s taking all the time and post it, someone would probably take a look and teach you a bit about writing fast Julia I’m the process.
