syntax.us Let the syntax do the talking
Blog Contact Posts Questions Tags Hire Me

Question:
In Ruby how do I read data from CSV file?

I am currently working on a use-case which reuires that I feed data from a CSV file into a chart served by D3.js

An early step in this process is to use Rails to read data from the CSV file.

The syntax below demonstrates the reading of data from a CSV file which looks like this:
1241136000,1.325
1241136300,1.324
1241136600,1.323
1241136900,1.322
1241137200,1.321
1241137500,1.325
1241137800,1.329
1241138100,1.325
1241138400,1.326
1241138700,1.327
require 'csv'
csvrows = CSV.read("db/small.csv")
p csvrows
rows4json = csvrows.map{|row| {:x =>row[0],:y =>row[1]} }
p rows4json.to_json



syntax.us Let the syntax do the talking
Blog Contact Posts Questions Tags Hire Me