{"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>\n

First 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>\n

Next 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>\n

Now 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>\n

Now, 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>\n

As 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":"\nHow to parse a CSV file using ruby<\/title>\n<meta name=\"description\" content=\"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\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/teamtutorials.com\/web-development-tutorials\/how-to-parse-a-csv-in-ruby\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Johnathan Ward\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebSite\",\"@id\":\"https:\/\/teamtutorials.com\/#website\",\"url\":\"https:\/\/teamtutorials.com\/\",\"name\":\"Team Tutorials\",\"description\":\"Learn something new today\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/teamtutorials.com\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/teamtutorials.com\/web-development-tutorials\/how-to-parse-a-csv-in-ruby#primaryimage\",\"inLanguage\":\"en-US\",\"url\":\"https:\/\/teamtutorials.com\/wp-content\/uploads\/2012\/09\/CSV.png\",\"contentUrl\":\"https:\/\/teamtutorials.com\/wp-content\/uploads\/2012\/09\/CSV.png\",\"width\":\"512\",\"height\":\"512\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/teamtutorials.com\/web-development-tutorials\/how-to-parse-a-csv-in-ruby#webpage\",\"url\":\"https:\/\/teamtutorials.com\/web-development-tutorials\/how-to-parse-a-csv-in-ruby\",\"name\":\"How to parse a CSV file using ruby\",\"isPartOf\":{\"@id\":\"https:\/\/teamtutorials.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/teamtutorials.com\/web-development-tutorials\/how-to-parse-a-csv-in-ruby#primaryimage\"},\"datePublished\":\"2012-09-14T19:47:52+00:00\",\"dateModified\":\"2015-03-16T20:39:03+00:00\",\"author\":{\"@id\":\"https:\/\/teamtutorials.com\/#\/schema\/person\/3abea1fc71644afe035403357450b9d9\"},\"description\":\"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\",\"breadcrumb\":{\"@id\":\"https:\/\/teamtutorials.com\/web-development-tutorials\/how-to-parse-a-csv-in-ruby#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/teamtutorials.com\/web-development-tutorials\/how-to-parse-a-csv-in-ruby\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/teamtutorials.com\/web-development-tutorials\/how-to-parse-a-csv-in-ruby#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/teamtutorials.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to parse a CSV file using ruby\"}]},{\"@type\":\"Person\",\"@id\":\"https:\/\/teamtutorials.com\/#\/schema\/person\/3abea1fc71644afe035403357450b9d9\",\"name\":\"Johnathan Ward\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/teamtutorials.com\/#personlogo\",\"inLanguage\":\"en-US\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/93d667fe5dc6df5c722e0df5eac14d40?s=96&d=mm&r=r\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/93d667fe5dc6df5c722e0df5eac14d40?s=96&d=mm&r=r\",\"caption\":\"Johnathan Ward\"},\"description\":\"Johnathan Ward is an experienced developer and consultant that writes tutorials to help other developers. In his day job, he is an IBM Watson Consultant with several years of experience deploying and customizing Watson Explorer solutions.\",\"sameAs\":[\"http:\/\/johnathanward.com\",\"https:\/\/twitter.com\/spyderman4g63\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to parse a CSV file using ruby","description":"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","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/teamtutorials.com\/web-development-tutorials\/how-to-parse-a-csv-in-ruby","twitter_misc":{"Written by":"Johnathan Ward","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebSite","@id":"https:\/\/teamtutorials.com\/#website","url":"https:\/\/teamtutorials.com\/","name":"Team Tutorials","description":"Learn something new today","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/teamtutorials.com\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"ImageObject","@id":"https:\/\/teamtutorials.com\/web-development-tutorials\/how-to-parse-a-csv-in-ruby#primaryimage","inLanguage":"en-US","url":"https:\/\/teamtutorials.com\/wp-content\/uploads\/2012\/09\/CSV.png","contentUrl":"https:\/\/teamtutorials.com\/wp-content\/uploads\/2012\/09\/CSV.png","width":"512","height":"512"},{"@type":"WebPage","@id":"https:\/\/teamtutorials.com\/web-development-tutorials\/how-to-parse-a-csv-in-ruby#webpage","url":"https:\/\/teamtutorials.com\/web-development-tutorials\/how-to-parse-a-csv-in-ruby","name":"How to parse a CSV file using ruby","isPartOf":{"@id":"https:\/\/teamtutorials.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/teamtutorials.com\/web-development-tutorials\/how-to-parse-a-csv-in-ruby#primaryimage"},"datePublished":"2012-09-14T19:47:52+00:00","dateModified":"2015-03-16T20:39:03+00:00","author":{"@id":"https:\/\/teamtutorials.com\/#\/schema\/person\/3abea1fc71644afe035403357450b9d9"},"description":"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","breadcrumb":{"@id":"https:\/\/teamtutorials.com\/web-development-tutorials\/how-to-parse-a-csv-in-ruby#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/teamtutorials.com\/web-development-tutorials\/how-to-parse-a-csv-in-ruby"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/teamtutorials.com\/web-development-tutorials\/how-to-parse-a-csv-in-ruby#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/teamtutorials.com\/"},{"@type":"ListItem","position":2,"name":"How to parse a CSV file using ruby"}]},{"@type":"Person","@id":"https:\/\/teamtutorials.com\/#\/schema\/person\/3abea1fc71644afe035403357450b9d9","name":"Johnathan Ward","image":{"@type":"ImageObject","@id":"https:\/\/teamtutorials.com\/#personlogo","inLanguage":"en-US","url":"https:\/\/secure.gravatar.com\/avatar\/93d667fe5dc6df5c722e0df5eac14d40?s=96&d=mm&r=r","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/93d667fe5dc6df5c722e0df5eac14d40?s=96&d=mm&r=r","caption":"Johnathan Ward"},"description":"Johnathan Ward is an experienced developer and consultant that writes tutorials to help other developers. In his day job, he is an IBM Watson Consultant with several years of experience deploying and customizing Watson Explorer solutions.","sameAs":["http:\/\/johnathanward.com","https:\/\/twitter.com\/spyderman4g63"]}]}},"_links":{"self":[{"href":"https:\/\/teamtutorials.com\/wp-json\/wp\/v2\/posts\/3015"}],"collection":[{"href":"https:\/\/teamtutorials.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/teamtutorials.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/teamtutorials.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/teamtutorials.com\/wp-json\/wp\/v2\/comments?post=3015"}],"version-history":[{"count":0,"href":"https:\/\/teamtutorials.com\/wp-json\/wp\/v2\/posts\/3015\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/teamtutorials.com\/wp-json\/wp\/v2\/media\/3019"}],"wp:attachment":[{"href":"https:\/\/teamtutorials.com\/wp-json\/wp\/v2\/media?parent=3015"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/teamtutorials.com\/wp-json\/wp\/v2\/categories?post=3015"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/teamtutorials.com\/wp-json\/wp\/v2\/tags?post=3015"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}