CSV Manipulation w/ Ruby

March 13, 2007

Lately, I’ve been trying to follow why’s advice by using Ruby at work whenever possible. I love Ruby because it’s very friendly to those who have little (or no) programming background. However, some people feel they have no place to use programming in their jobs. Well, let’s look at the example of processing a csv file (something most of use have done). Suppose we have a file with categories and totals. Assume we want to find unique categories and the sum of the totals for each category.

1. Let’s first install the FasterCSV gem. It’s easier to use and faster (duh) than Ruby’s standard csv library. Install it by doing the following from the command line: gem install fastercsv

2. Ok, so we now have fastercsv installed. Assume our file is called “orig.csv”. Here’s a script that will do what we described above. Take a look at the code and I’ll describe it in detail below.

Code

Description: In a nutshell, we include fastercsv in our code, read through our file and and stick the category-total pairs into a hash and then write out the totals to a new file. The only tricky part is line 11, where we check to see if an entry for the current category exists. If not, we create one with a total of 0, otherwise we do nothing. If you have specific questions, please post a comment and I’ll respond.

Advertisements

3 Responses to “CSV Manipulation w/ Ruby”

  1. christian Says:

    Hello,

    do you know a possinility to change the
    delimiter, because in europe it is “;” instead
    of “,” and sometimes i need tab “\t”.
    I look into the example of faster_csv but find nothing.

    many thanks ,Christian

  2. drewolson Says:

    Christian –

    This is part of the options when reading a csv file. In order to read a csv with a delimiter of “;” one would simply do:

    FCSV.foreach(“myfile.csv”, :col_sep => “;”) do |row|
    #do your work
    end

    Thanks it! Hope that helps.

    -Drew

  3. Kwami A Says:

    Awesome! you’ve just given me one more reason to LOVE ruby! I’ve discovered this little treasure not long ago, and I must say… It makes those repetitive boring tasks a hell much more enjoyable 🙂


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: