{"id":3015,"date":"2012-09-14T15:47:52","date_gmt":"2012-09-14T19:47:52","guid":{"rendered":"http:\/\/teamtutorials.com\/?p=3015"},"modified":"2015-03-16T16:39:03","modified_gmt":"2015-03-16T20:39:03","slug":"how-to-parse-a-csv-in-ruby","status":"publish","type":"post","link":"https:\/\/teamtutorials.com\/web-development-tutorials\/how-to-parse-a-csv-in-ruby","title":{"rendered":"How to parse a CSV file using ruby"},"content":{"rendered":"
Here’s a simple tip on how to parse a comma separated value or CSV file using ruby. Ruby has a standard library available that makes this task very simple. All we need to do is open a CSV file, read it, and parse it using the CSV parser that comes with ruby. Let’s say we want to import a CSV file that contains the fields: name,age,sex.<\/p>\n
\r\nname,age,sex\r\njohn,28,m\r\njulie,19,f\r\nadrienne,42,f\r\nrob,16,m\r\n<\/pre>\nFirst off I create the csv file above and saved it as test.csv. Next we need to get started with ruby.<\/p>\n
In order to parse this CSV I am going to use the build in CSV library, so we need to require the CSV library.<\/p>\n
\r\nrequire 'csv'\r\n<\/pre>\nNext we need to read the file and store it to a variable<\/p>\n
\r\nrequire 'csv'\r\n\r\ncsv_text = File.read('test.csv')\r\n<\/pre>\nNow we’re going to tell the CSV library to parse the file, and that the file has headers. When we specify that headers exist, the parse will use the first row as the names for each value.<\/p>\n
\r\nrequire 'csv'\r\n\r\ncsv_text = File.read('test.csv')\r\ncsv = CSV.parse(csv_text, :headers => true)\r\n<\/pre>\nNow, we just need to loop through each record and perform whatever task it is we want to do with the data. In this case I just output to the terminal window using puts.<\/p>\n
\r\nrequire 'csv'\r\n\r\ncsv_text = File.read('test.csv')\r\ncsv = CSV.parse(csv_text, :headers => true)\r\n\r\ncsv.each do |row|\t\t\t\t\t\t\t\t\t\t\r\n\tputs "Name: #{row['name']} - Age: #{row['age']} - Sex: #{row['sex']}"\r\nend\r\n<\/pre>\nAs you can see above when we run: csv.each do |row|, that sets the variable row to an array of all the values in the row. It also will use the names from the first column. So to get a certain field we just need to use row[‘fieldname’].<\/p>\n
When you run this in the terminal (ruby csv.rb) you should see something like this:<\/p>\n
\r\njward-laptop:Desktop jward$ ruby csv.rb\r\nName: john - Age: 28 - Sex: m\r\nName: julie - Age: 19 - Sex: f\r\nName: adrienne - Age: 42 - Sex: f\r\nName: rob - Age: 16 - Sex: m\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"Here’s a simple tip on how to parse a comma separated value or CSV file using ruby. Ruby has a standard library available that makes this task very simple. All we need to do is open a CSV file, read it, and parse it using the CSV parser that comes with ruby. Let’s say we … Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":3019,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[156,17],"tags":[93,94,155,152,161],"yoast_head":"\n
How to parse a CSV file using ruby<\/title>\n\n\n\n\n\t\n\t\n\t\n