# Append a single line to a csv file for a multiple of times using Julia

**URL:** <https://discourse.julialang.org/t/append-a-single-line-to-a-csv-file-for-a-multiple-of-times-using-julia/24200>\
**Category:** General Usage\
**Created:** [May 14, 2019, 8:29am UTC](https://discourse.julialang.org/t/append-a-single-line-to-a-csv-file-for-a-multiple-of-times-using-julia/24200 "2019-05-14T08:29:42Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![bsnyh](https://avatars.discourse-cdn.com/v4/letter/b/ce7236/32.png) [@bsnyh](https://discourse.julialang.org/u/bsnyh)\
**Post date:** [May 14, 2019, 8:29am UTC](https://discourse.julialang.org/t/append-a-single-line-to-a-csv-file-for-a-multiple-of-times-using-julia/24200/1 "2019-05-14T08:29:42Z")

</div>

Hi, my task is to append a single line to a given csv file for a couple of times using Julia. There might be more than one ways to do it. I was thinking to use the DataFrames module and the CSV module to accomplish this. The following is the initial code, which is not working so far.

```julia
using DataFrames
using CSV

df = DataFrame(A=1:3, B=4:6, C=7:9)

for i in 1:10 # do the following for 10 times
    CSV.write("append_a_line.csv", df, header = true, append = true)
end

```

When running the above code, I got an error as the following:  
“TypeError: in #write, in typeassert, expected Array{T,1} where T, got Bool”.

Could anyone kindly let me know what might be wrong here? Or do you have any other better ways to handle this?  
Many thanks for your time and attention.

---

<div class="post-metadata">

**Author:** ![Jonathan\_Droxler](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jonathan_droxler/32/9451_2.png) [@Jonathan\_Droxler](https://discourse.julialang.org/u/Jonathan_Droxler)\
**Post date:** [May 14, 2019, 9:10am UTC](https://discourse.julialang.org/t/append-a-single-line-to-a-csv-file-for-a-multiple-of-times-using-julia/24200/2 "2019-05-14T09:10:20Z")

</div>

Hi,

So you want to append your 3x3 Dataframe 10 times correct?

Don’t know if there is a better way but you cannot use

```julia
header=true

```

If you look at documentation for CSV.write, you will read

```
• header: pass a list of column names (Symbols or
    Strings) to use instead of the column names of the
    input table

```

So you can remove this parameter and it will work.  
If you want to have the header printed, It seems that using append=true doesn’t create header, so I suggest that you write

```nohighlight
using CSV

df = DataFrame(A=1:3, B=4:6, C=7:9)

CSV.write("append_a_line.csv", df, delim = ';')
for i in 1:9 # do the following for 10 times
    CSV.write("append_a_line.csv", df, delim = ';',append=true)
end

```

You can add the parameter

```julia
delim = ';'

```

such that your column will be correctly separate.

But there is maybe a nicer way to do it.

Is this answer your question?

Regards

---

<div class="post-metadata">

**Author:** ![bsnyh](https://avatars.discourse-cdn.com/v4/letter/b/ce7236/32.png) [@bsnyh](https://discourse.julialang.org/u/bsnyh)\
**Post date:** [May 14, 2019, 9:25am UTC](https://discourse.julialang.org/t/append-a-single-line-to-a-csv-file-for-a-multiple-of-times-using-julia/24200/3 "2019-05-14T09:25:44Z")

</div>

[https://github.com/JuliaData/CSV.jl/issues/247](https://github.com/JuliaData/CSV.jl/issues/247)  
I’m following the above link basically. It seems like this issue has already been solved. However, I do not quite follow the way to solve it.

---

<div class="post-metadata">

**Author:** ![bernhard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bernhard/32/2619_2.png) [@bernhard](https://discourse.julialang.org/u/bernhard)\
**Post date:** [May 14, 2019, 10:53am UTC](https://discourse.julialang.org/t/append-a-single-line-to-a-csv-file-for-a-multiple-of-times-using-julia/24200/4 "2019-05-14T10:53:11Z")

</div>

try this (it seems that the keyword header is not the correct one)

I assumed you only want to write the header once.

```julia
using DataFrames
using CSV

df = DataFrame(A=1:3, B=4:6, C=7:9)

fi="c:\\temp\\append_a_line.csv"
fi="append_a_line.csv"

for i in 1:10 # do the following for 10 times    
    CSV.write(fi, df, writeheader = (i==1), append = true)
end

```

---

<div class="post-metadata">

**Author:** ![bsnyh](https://avatars.discourse-cdn.com/v4/letter/b/ce7236/32.png) [@bsnyh](https://discourse.julialang.org/u/bsnyh)\
**Post date:** [May 14, 2019, 12:39pm UTC](https://discourse.julialang.org/t/append-a-single-line-to-a-csv-file-for-a-multiple-of-times-using-julia/24200/5 "2019-05-14T12:39:54Z")

</div>

@bernhard, thank you so much for your quick reply. I tried it out and it worked well. However, it is not exactly what I needed. Instead of controlling the number of times the `CSV.write` function is called, I need to put this `CSV.write` inside of another function, say `myFunction`. Then, the first time when this function `myFunction` is called, a single line with header is written. After that, for all other times when the function `myFunction` is called, only a single line is written, without the header. I guess I can use a global flag variable to indicate the number of times the function `myFunction` is called. And then it is doable in this way. At the same time, I was wondering, are there any other more elegant way to do this then?

---

<div class="post-metadata">

**Author:** ![bernhard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bernhard/32/2619_2.png) [@bernhard](https://discourse.julialang.org/u/bernhard)\
**Post date:** [May 14, 2019, 12:52pm UTC](https://discourse.julialang.org/t/append-a-single-line-to-a-csv-file-for-a-multiple-of-times-using-julia/24200/6 "2019-05-14T12:52:00Z")

</div>

Well, do you know the header (i.e. the column names)?  
If yes, you can simply do

```julia
myHeader=["colA","colB","somethingFancy"]
myHeaderReshaped=reshape(myHeader,1,length(myHeader))
using DelimitedFiles

#note: this will 'delete/overwrite' the file if it already exists
writedlm(fi,myHeaderReshaped,',') #note: last argument is the delimiter which should be the same as below

#a lot of code and nested functions......
#.....

for i in 1:10 # do the following for 10 times    
    CSV.write(fi, df, writeheader = false, append = true,sep=',')
end

```

---

<div class="post-metadata">

**Author:** ![bsnyh](https://avatars.discourse-cdn.com/v4/letter/b/ce7236/32.png) [@bsnyh](https://discourse.julialang.org/u/bsnyh)\
**Post date:** [May 14, 2019, 12:54pm UTC](https://discourse.julialang.org/t/append-a-single-line-to-a-csv-file-for-a-multiple-of-times-using-julia/24200/7 "2019-05-14T12:54:42Z")

</div>

Nice job. But why

```julia
myHeaderReshaped=reshape(myHeader,1,length(myHeader))

```

---

<div class="post-metadata">

**Author:** ![bernhard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bernhard/32/2619_2.png) [@bernhard](https://discourse.julialang.org/u/bernhard)\
**Post date:** [May 14, 2019, 1:01pm UTC](https://discourse.julialang.org/t/append-a-single-line-to-a-csv-file-for-a-multiple-of-times-using-julia/24200/8 "2019-05-14T13:01:04Z")

</div>

Well, you can try the code without that line.  
writedlm seems to expect an Array of dimension two. If you simply provide it with the vector, it will write the header on 3 lines.

---

<div class="post-metadata">

**Author:** ![JosephPollacco](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/josephpollacco/32/51404_2.png) [@JosephPollacco](https://discourse.julialang.org/u/JosephPollacco)\
**Post date:** [April 27, 2022, 9:02pm UTC](https://discourse.julialang.org/t/append-a-single-line-to-a-csv-file-for-a-multiple-of-times-using-julia/24200/9 "2022-04-27T21:02:59Z")

</div>

Thanks for trying to resolve the issue. But I tested and the solution and it does not seem to work for me, I am wandering of I am doing something wrong. When I append the **header** does not write down. Strangely it worked once but then failed to work. I am pasting below my code:

```julia
   for iClimateInventory in ClimateInventory

      Data = CSV.File(ClimatePath_Input, header=true)

      Year_Obs_Start = convert(Vector{Float64}, Tables.getcolumn(Data, :Year_Obs_Start))
      Month_Obs_Start = convert(Vector{Float64}, Tables.getcolumn(Data, :Month_Obs_Start))
      Day_Obs_Start = convert(Vector{Float64}, Tables.getcolumn(Data, :Day_Obs_Start))

      Year_Obs_End = convert(Vector{Float64}, Tables.getcolumn(Data, :Year_Obs_End))
      Month_Obs_End = convert(Vector{Float64}, Tables.getcolumn(Data, :Month_Obs_End))
      Day_Obs_End = convert(Vector{Float64}, Tables.getcolumn(Data, :Day_Obs_End))

      Year_Sim_Start = convert(Vector{Float64}, Tables.getcolumn(Data, :Year_Sim_Start))
      Month_Sim_Start = convert(Vector{Float64}, Tables.getcolumn(Data, :Month_Sim_Start))
      Day_Sim_Start = convert(Vector{Float64}, Tables.getcolumn(Data, :Day_Sim_Start))

      Year_Sim_Start = convert(Vector{Float64}, Tables.getcolumn(Data, :Year_Sim_Start))
      Month_Sim_Start = convert(Vector{Float64}, Tables.getcolumn(Data, :Month_Sim_Start))
      Day_Sim_Start = convert(Vector{Float64}, Tables.getcolumn(Data, :Day_Sim_Start))

      Hour_Start=[9]
      Hour_Start_Sim=[9]
      Hour_End =[9]

      NEW_FILE(iClimateInventory, Year_Obs_Start, Year_Sim_Start, Year_Obs_End ,Month_Obs_Start, Month_Sim_Start ,Month_Obs_End, Day_Obs_Start ,Day_Sim_Start ,Day_Obs_End ,Hour_Start,Hour_Start_Sim ,Hour_End, i)
   end
   
return nothing
end # function: JULES_CLIMATE
# ------------------------------------------------------------------

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# FUNCTION : NEW_FILE
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function NEW_FILE(iClimateInventory, Year_Start, Year_Start_Sim, Year_End ,Month_Start, Month_Start_Sim ,Month_End, Day_Start ,Day_Start_Sim ,Day_End ,Hour_Start, Hour_Start_Sim ,Hour_End, i)

   ClimatePath_Output = joinpath(raw"D:\DATAraw\JULES_DATA\DATE\DATE_SUMMARY.csv" )
      
   Header = ["Station", "Year_Start" ,"Year_Start_Sim", "Year_End" , "Month_Start" ,"Month_Start_Sim" ,"Month_End" ,"Day_Start", "Day_Start_Sim" ,"Day_End" ,"Hour_Start" ,"Hour_Start_Sim", "Hour_End"]

   CSV.write(ClimatePath_Output, Tables.table([iClimateInventory Year_Start Year_Start_Sim Year_End Month_Start Month_Start_Sim Month_End Day_Start Day_Start_Sim Day_End Hour_Start Hour_Start_Sim Hour_End]), bom=true, append=true, writeheader = true,header=Header, delim = ",")

      
   return nothing
end # function: NEW_FILE
# ------------------------------------------------------------------

```
