How To Do Basic CSV Manipulations In Ruby (RubyShorts)

 - Webdesign Mechelen

Video

https://www.youtube.com/watch?v=g4BzgFYtrAs

Opening/Creating CSV File

First up, require the CSV library in your file. You don't need to install any gem for it, it comes bundled with your installation of ruby.

require "csv"

Then, to create a new CSV file you're going to;

csv = CSV.open("people.csv", "a+")

This will "open" a CSV file called "people.csv" in read-write mode where new writes will be added to the back of the file (append). If the file doesn't exist yet, it'll create it. Here's an overview of the different modes;

Modes Description
r Read-only mode. The file pointer is placed at the beginning of the file. This is the default mode.
r+ Read-write mode. The file pointer will be at the beginning of the file.
w Write-only mode. Overwrites the file if the file exists. If the file does not exist, creates a new file for writing.
w+ Read-write mode. Overwrites the existing file if the file exists. If the file does not exist, creates a new file for reading and writing.
a Write-only mode. The file pointer is at the end of the file if the file exists. That is, the file is in the append mode. If the file does not exist, it creates a new file for writing.
a+ Read and write mode. The file pointer is at the end of the file if the file exists. The file opens in the append mode. If the file does not exist, it creates a new file for reading and writing.

Adding Data To CSV

# Adding new data
headers = ["name","height", "age","gender"]
CSV.open('file.csv', 'a+') do |row|
 row << headers
end

Reading Data From CSV

# Reading data from CSV
file = CSV.read('exercises.csv', headers: true, header_converters: :symbol, converters: :all)
file.each_with_index do |row, i|
 name = row[:name]
 equipment = row[:equipment]
end

# OR

file = CSV.read('exercises.csv', headers: true, header_converters: :symbol, converters: :all)
file.map {|row| row.to_hash}
=> [{:name=>"Landmine 180's",
  :equipment=>"Barbell",
  :focus=>"Abdominals", 
   ... }]

Options

headers: true # Removes headers from read 
header_converters: :symbol # Allows each header to be accessed by their respective symbol 
converters: :all # Converts each datatype to the correct ruby datatype ex: string representations of integers get converted into integer

Resources


Questions/Suggestions?

Related Articles

 - Webdesign Mechelen

How to Upload Subscribers to Mailchimp Using CSV File (RubyShorts)

Ever wanted to bulk upload users to your mailchimp account but were hindered because of the omnivore alert? Well with some magical ruby code and an API-key you won't have any problems :)

 - Webdesign Mechelen

How To Install OneNote On Ubuntu (2017)

Do you love keeping your notes in OneNote, made by Microsoft and can't really live without it? Well in this video I'm going to show you how you can set it up on your linux ubuntu device.

 - Webdesign Mechelen

How To Create An Automatic Sitemap For Your Rails App On Heroku (RailsShorts)

Wish your sitemap was automatically updated once a week or faster without having to manually update it and push the changes to your server? Combine sitemap generator & fog to fix this!

 - Webdesign Mechelen

How To Handle Errors In Ruby With Begin, Rescue & Ensure (RubyShorts)

Are your trying to catch some errors in your ruby application but can't really wrap your head around the begin, rescue and ensure blocks in ruby? Here are some pointers!

 - Webdesign Mechelen

How To Query A Basic API In Ruby (RubyShorts)

Here's a quick article on how you can quickly retrieve data from an API endpoint using the open-uri and json library

How to Upload Subscribers to Mailchimp Using CSV File (RubyShorts)

Ever wanted to bulk upload users to your mailchimp account but were hindered because of the omnivore alert? Well with some magical ruby code and an API-key you won't have any problems :)

How To Install OneNote On Ubuntu (2017)

Do you love keeping your notes in OneNote, made by Microsoft and can't really live without it? Well in this video I'm going to show you how you can set it up on your linux ubuntu device.

How To Create An Automatic Sitemap For Your Rails App On Heroku (RailsShorts)

Wish your sitemap was automatically updated once a week or faster without having to manually update it and push the changes to your server? Combine sitemap generator & fog to fix this!

How To Handle Errors In Ruby With Begin, Rescue & Ensure (RubyShorts)

Are your trying to catch some errors in your ruby application but can't really wrap your head around the begin, rescue and ensure blocks in ruby? Here are some pointers!

How To Query A Basic API In Ruby (RubyShorts)

Here's a quick article on how you can quickly retrieve data from an API endpoint using the open-uri and json library