Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Julia Parsing a CSV

Hope the evening is a more than hospitable one and you have traded your emacs terminal for some bonne-vivant Ralph Lauren catalog dinner party type scene. As for me, I'm trying to parse a CSV in Julia and things are deteriorating. Here is my code:

f2 = open("/Users/MacBookPro15/testnovo.csv", "r")

skip(f2, 736)

for line in eachline(f2)
    string_split = split(line, ",")
    println(string_split[1])      
end

Now if I substitute string_split[2] or anything other than [1] I get a BoundsError and it's rather frustrating because I need those items. Can anyone tell me how to avoid this?

like image 265
Chase CB Avatar asked Dec 09 '25 09:12

Chase CB


1 Answers

Every time I hear "parsing a CSV" I want to duck and cover my ears, before I get flashbacks of a missing quote, or a 32-column line 98% of the way through a 33-column, 10GB csv file.

Fortunately, there are two useful functions that'll prevent you from rolling your own csv parser:

  • readcsv in Julia's standard library http://docs.julialang.org/en/release-0.2/stdlib/base/?highlight=readcsv#Base.readcsv
  • readtable in Dataframe.jl http://juliastats.github.io/DataFrames.jl/io.html
like image 92
Ben Hamner Avatar answered Dec 12 '25 01:12

Ben Hamner



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!