How do i get CR+LF ('\r\n') line endings using CSV.jl?


#1

Hi,
I would like to write a DataFrame to a csv-file in DOS-style (’\r\n’).
Something like
CSV.write(csv_file, DataFrame, delim=’;’)
writes the usual ‘\n’ to the end of the line, but I need ‘\r\n’ line endings and couldn’t find a parameter to do it. How can I do it ?

Thank you for helping me.


#2

This works (although it can probably be improved in various ways).
The conversion to an Array{Any,2} might be superfluous.

using DataFrames
using DelimitedFiles 

x=["a" 1;"b" 2.3;"maha" 22]
df=DataFrame(A=String.(x[:,1]),B=Float64.(x[:,2]))

arr=convert(Array{Any,2},df)

fi="C:\\temp\\file2.csv"
isfile(fi)&&rm(fi)
fiostream=open(fi,"a+")

rows,cols=size(arr)
sep=','
for i=1:rows
    v=view(arr,i,:)
    for j=1:cols
        elm=string.(v[j])
        write(fiostream,elm)
        j<cols&&write(fiostream,sep)
    end    
    write(fiostream,"\r\n")
end

close(fiostream)

#3

Hi bernhard,

thank you for your work around. In the meantime I found a pull request for this issue: https://github.com/JuliaData/CSV.jl/pull/376