Why watching ads can be useful?

You got to have an overview of everyone around you to be prepared, be ready for new striving companies.🏰 We used to ignore most of the content from advertising, there is nothing wrong with it. Why…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Parsing a CSV File in Ruby

CSV stands for comma separated values which is commonly seen in excel spreadsheets. While on a program like excel the data will appear with columns and rows inside its own cells, once viewed as raw code the CSV file will look something like this.

Ruby has a built in library that allows us to do many things with CSV files. To begin go ahead and create a new file and add

at the top. Now that you can use an entire set of methods to do what you’d like to interact with your csv file. To simply read your csv file you can go ahead and enter the following code.

or you can parse it

These methods however do not account for the fact that our CSV file has headers. In order to include our headers we can go with another approach. These methods are also no good if our file was large, say greater than 10 MB. To bypass these issues the best approach would be to use a foreach method and with a block and read the file line by line. This way the program will be faster and use less memory. This approach would look something like this.

Now you can go ahead and play around with this script and I suggest viewing each row by column name or index like so to see what your code will output to get a better idea of how things are working.

From here you can get creative and do whatever you’d like with your CSV file. Go head and try iterating through each row and adding some data into an array while the foreach method runs through your CSV file.

Now you have an array with all the data from your first column. With this array we could now go ahead and create a new CSV file.

With the method CSV.open, the first argument is the name of the csv file that will be outputted and the second parameter ‘wb’ indicates that the program will write a new csv file. From here we create our first line which is a header called name. We shovel that in to the variable within our block called csv and add that to our table. Next we loop through the array of all the names we collected earlier and add them into the csv. We run the loop according to how large our array is. After this you can look at your project directory and you’ll see your new CSV file outputted.

To close, there are many ways you can manipulate your CSV files and how you’d like to output them. By using what was taught in this article you could now go ahead and maybe parse a CSV file and use that data to seed your database in your rails API. Or you could even convert your parsed data in JSON format. The possibilities are endless.

Add a comment

Related posts:

Introduction to Scrum and Agile

Hey fellow coders! Just a heads up, this article is part of my learning process, so I won’t be diving too deep into the topic. But hopefully it’ll still be helpful for anyone who’s interested in…

Finding quality people to believe in me

I wanted to really put a beat on things not just going on in SV (Silicon Valley) but In life. A certain place is just where people meet. Silicon Valley is the best place I have ever lived other then…

Mindset Coaching Helped Me Become More Mindful and Find Inner Peace.

I have recently completed a program called Burnout to Badass, with coaching provided by Tanvi Chadha of We Wear Wellness. On her website she states her mission as, “I help ambitious women who…