Given a (2d) array A
how to export it into a CSV file with Julia?
Back in the older versions, I would write
writecsv( "FileName.csv", A);
which would dump the array A into the given file. This, however, does not work in version >= 1.0. After some googling, I also tried to use the CSV module doing
f = open("test.csv", "a+");
CSV.write(f, A; delim = ',')
but this throws an error
ERROR: ArgumentError: no default `Tables.rows` implementation for type: Array{Int64,2}
(my array A was of type Int64).
Does anyone have a working solution for this most trivial question?
For the final step, you may use the template below in order to export your DataFrame to CSV in Julia: For our example, the path where the CSV file will be stored on my computer is: C:\Users\Ron\Desktop\Test\export_df.csv Where ‘ export_df ‘ is the new file to be created, and ‘ .csv ‘ is the file extension.
Yes, CSV files are nothing but simple text files. , Columns are separated with commas. Rows are separated with newlines. All right, let us now get into the examples of how to “export” an array to a CSV file in Javascript.
Julia expects all the strings to be UTF-8 encoded. If it’s not the case, you must first open the file using its encoding, then pass it to CSV.File and then to the data frame. You can use open (read, path, encoding) or shortcut to read (path, encoding).
CSV loaded to the Julia DataFrame displayed in Jupyter notebook. Image by Author. You can see that Julia representation (unlike python pandas) displays the data type of the column, whether it is a string, int or float. The second possibility is to use Julia’a pipe operator |> to pass the CSV.File to a DataFrame. The result is the same.
You need to load DelimitedFiles
module and now only writedlm
function is supported.
So in order to write an array as CSV file use:
julia> using DelimitedFiles
julia> writedlm( "FileName.csv", A, ',')
To get the same result with CSV.jl package use:
julia> using CSV, Tables
julia> CSV.write("FileName.csv", Tables.table(A), writeheader=false)
as Matrix
does not support Tables.jl interface so you need to wrap it with Tables.table
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With