Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to parse CSV files with double-quoted strings in Julia?

Tags:

string

csv

julia

I want to read CSV files where the columns are separated by commas. The columns can be strings and if those strings contain a comma in their content, they are wrapped in double-quotes. Currently I'm loading my data using:

file = open("data.csv","r")
data = readcsv(file)

But this code code would split the follwing string into 4 pieces whereas it only should be 3:

1,"text, more text",3,4

Is there a way in Julia's Standard Library to parse CSV while respecting quoting or do I have to write my own custom solution?

like image 399
Uwe L. Korn Avatar asked Jan 18 '26 14:01

Uwe L. Korn


1 Answers

The readcsv function in base is super-basic (just blindly splitting on commas).

You will probably be happier with readtable from the DataFrames.jl package: http://juliastats.github.io/DataFrames.jl/io.html

To use the package, you just need to Pkg.add("DataFrames"), and then import it with `using DataFrames"

like image 122
astrieanna Avatar answered Jan 21 '26 07:01

astrieanna