Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby

How to read data from csv file in ruby

I have written the code

require 'csv'
  sam = CSV.foreach('./csv_test.csv') do |row|
    puts row.inspect
  end

This CSV file file format looks like

TS No.,Desc,Date,Result
1,Test1,26/12/2015,passed
2,Test2,29/12/2015,passed
3,Test3,28/12/2015,failed

It will read like this ["TS No.", "Desc", "Date", "Result"] ["1", "Test1", "26/12/2015", "passed"] ["2", "Test2", "29/12/2015", "passed"] ["3", "Test3", "28/12/2015", "failed"]

But the code I should print like this TS NO. 1 Desv Test1 Date 26/12/2015 Result passed

TS NO. 2 Desv Test2 Date 29/12/2015 Result passed

TS NO. 3 Desv Test3 Date 28/12/2015 Result failed

1 Answer

Here's a tutorial from SitePoint on reading a csv file:

http://www.sitepoint.com/guide-ruby-csv-library-part/