Bilal Haider Qureshi avatar

Ruby Programming Tutorial - Lesson 42 - Working with CSV files in ruby

bilal-haider

Published: 29 Mar 2018 › Updated: 29 Mar 2018Ruby Programming Tutorial - Lesson 42 - Working with CSV files in ruby

Ruby Programming Tutorial - Lesson 42 - Working with CSV files in ruby

cropped-data-management-hero.jpg

CSV or Comma separated files

Comma separated files are widely used, people use them to create their company data, or use to create financial statements yet they have so many uses.

The quesiton

How can we handle CSV files in ruby ?

We all have used Microsoft excel at some point of time in our lives, or if we are on ubuntu, we use Libre Office calc,
handling this type of files can also be done via regular filing, but there is a better solution which ruby provides us to handle such file.

csv-sheets-example.png

Ruby provides us a Class "CSV" :)

This class provides a complete interface to CSV files and data. It offers tools to enable you to read and write to and from Strings or IO objects, as needed.

Lets don't wait any further and take a programming example, which will be very helpful when dealing with CSV files.


CSV.foreach("file.csv") do |row|
    ## p is similar to puts its
    puts "Here is a Row of Data ==> " 
    p row
end

This file type is so cool and with CSV class which ruby provides us it becomes an amazing tool.
we can use csv files to store persistent data of our ruby applications if we want to :)
With ruby class, it becomes very easy to handle such files, when reading them line by line or all at once.

Screenshot from 2018-03-29 18-02-23.png

Here is how you read all the data at once, which returns you an Array of arrays :)

require 'csv'

p CSV.read("file.csv")

Screenshot from 2018-03-29 18-13-54.png

If we have very large data, which consists upon maybe 1 million records, its good idea to construct a Binary search tree from the data, so that you can search for a record in an efficient way. We studied binary search trees in previous Articles.

If we wish to write data to CSV files, we can do so, by doing this..

require 'csv'

## Writing to a CSV file. 

CSV.open("file.csv", "w") do |csv|
    csv << ["1", "Bilal Haider", "27", "Islamabad, Pakistan"]
    csv << ["2", "Bagosan", "25", "Belgium, Belgium"]
    csv << ["3", "Danish Khan", "25", "Islamabad, Pakistan"]
    csv << ["4", "Bilal khan", "26"]
    csv << ["5", "Walter king", "20", "Islamabad, Pakistan"]
    csv << ["6", "Sazish khan", "60", "Islamabad, Pakistan"]
end

Screenshot from 2018-03-29 18-19-54.png

If you are building a computer game, where you need to store player's scores you can use such type of a file but you don't have to store scores and related data as plain-text, you can use encryption for that as well. That makes your data more secure.

It is best practice to use this kind of files for tabular data

if you want to learn more about CSV Class about what other methods, it provides that you can use in your ruby programs, check out this reference link :)
https://ruby-doc.org/stdlib-2.5.0/libdoc/csv/rdoc/CSV.html

Leave Ruby Programming Tutorial - Lesson 42 - Working with CSV files in ruby to:

Written by

I am a programmer :)

Read more #ruby posts


Best Posts From Bilal Haider Qureshi

We have not curated any of bilal-haider's posts yet. But you can encourage our curation team to review posts by visiting them regularly and by referring other readers. Because we give priority to frequently read content.

More Posts From Bilal Haider Qureshi