Ruby Programming Tutorial - Lesson 42 - Working with CSV files in ruby
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.
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.
Here is how you read all the data at once, which returns you an Array of arrays :)
require 'csv'
p CSV.read("file.csv")
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
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:
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
- Roar!! is what bearshares' new frontend called
- Join bearshares.com and get 50 SP signup bonus, + invite your friends and earn 50 BEARS for each of them
- Bearjobs (jobs.bearshares.com)
- Bearshares now has BearWallet, Advertisement Network and Hyip Investment Program
- bearshares.com reached 1100 members :)
- Inviting everyone to bearshares.com ==> rebirth of paying social network
- Bearshares launched its own explorer
- https://bearshares.com giving 50 SP sign up bonus
- steem 0.20.3 first problem !
- Installed Condenser on https://beryxchange.com
- Ruby Programming for Beginners ==> Part 11 Video
- Ruby Programming for Beginners ==> Part 10 Video
- Ruby Programming for Beginners ==> Part 9 Video
- Ruby Programming for Beginners ==> Part 8 Video
- Ruby Programming for Beginners ==> Part 7 Video
- Ruby Programming for Beginners ==> Part 6 Video
- Ruby Programming for Beginners ==> Part 5 Video
- Ruby Programming for Beginners ==> Part 4 Video
- Ruby Programming for Beginners ==> Part 3 Video
- Ruby Programming for Beginners ==> Part 2 Video