Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Export an array to a CSV file in Julia

Tags:

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?

like image 540
Hayk Avatar asked Oct 19 '18 21:10

Hayk


People also ask

How to export Dataframe to CSV in Julia?

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.

Can I export an array to a CSV file in JavaScript?

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.

How do I read a string from a CSV file in Julia Julia?

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).

How to load a CSV file into a Jupyter Dataframe?

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.


1 Answers

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.

like image 95
Bogumił Kamiński Avatar answered Oct 20 '22 05:10

Bogumił Kamiński