Post

Writing Array Elements to CSV in Ruby

There is nothing more frustrating than realizing that a ruby script failed when processing, and you do not know where it failed and where to pick up the processing. Having the ability to write results or values out to a static file is very useful to identify where a script failed.

Writing results out to a CSV file is a small thing that can dramatically change how long it takes you to restart your script.

Writing the array element to CSV

The code snippet below tells Ruby to use the CSV gem to open the processed.csv file and uses the "a" flag to append to the CSV file. Once it reads the file, it adds the array element (‘e’) to the CSV file. This addition takes almost no time to add, but can save you a lot of hunting in your file to see where to pick up.

1
2
3
4
5
require 'csv'

CSV.open("processed.csv", "a") do |csv|
    csv << e
end

There are a few modes for you to use when opening files in Ruby. Here is a link to the IO class documentation for the different file opening modes. I’m including the details from the table in the documentation below.

Mode Flag Mode Description
“r” Read-only, starts at beginning of file (default mode).
“r+” Read-write, starts at beginning of file.
“w” Write-only, truncates existing file to zero length or creates a new file for writing.
“w+” Read-write, truncates existing file to zero length or creates a new file for reading and writing.
“a” Write-only, each write call appends data at end of file. Creates a new file for writing if file does not exist.
“a+” Read-write, each write call appends data at end of file. Creates a new file for reading and writing if file does not exist.

CSV and Watir Example

As an example, you have a file with 5,400 records in a file. The script uses the Watir gem to go open up a browser, navigate to a website, lookup the records, update a value on the record, and click the save button. Your script has the potential to fail if the website has a hiccup or an unexpected error is displayed on the screen.

Below is some example code that sets up the example. If the website were to fail after the 300th record and the terminal were to throw thousands of lines of exceptions, you might not be able to scroll to where the puts statement left off.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
require 'watir'
require 'csv'

newList = CSV.read("original_dataset.csv").drop(1)
b = Watir::Browser.new :chrome
b.goto 'https://www.example.com'

newList.each do |e|
b.text_field(id: 'text13v2').set 'e[0]
b.text_field(id: 'text14v2').clear
b.text_field(id: 'text14v2').set e[1]
b.button(id: 'button18v2').click
puts 'Processed: #{e[0]} - #{e[1]}."

    CSV.open("processed.csv", "a") do |csv|
        csv << e
    end

end

However, with the three lines we added after the button is clicked and the puts command logs some information in the console, we are in a much better spot.

Conclusion

Writing information to a CSV file is a very easy and quick addition to ruby scripts that can save you time when running a script on a large dataset. Since Ruby doesn’t store any information in memory when the script fails, having this information available to you in real time as the script processes is invaluable.

This post is licensed under CC BY 4.0 by the author.