Wednesday, September 2, 2009

Super Sweet CSV parsing with Perl

Today, everyday, every other day I need to pull in CSV files and write others out. After awhile, I was ready for a better solution. Lucky for me, the Perl community is amazing.

For some amazing parsing options look no further than Text::CSV_XS, it provides a super sweet parsing interface. No talking just pure Perl code:
use strict;
use warnings;

use Text::CSV_XS;

my $csv = Text::CSV_XS->new( {eol => "\n"} ); # eol has nothing to do with parsing wait until later :)
my $input = shift || die "Missing filename\n";

open my $fh, '<', $input
or die "Failed to open $input: $!\n";

# assume no file header
while ( my $columns = $csv->getline($fh) ) {
print $columns->[0], $columns->[1], "\n"; # or whatever you want to do

close $csv;

That's for parsing. You can do some other super cool stuff too, with bind_columns. Lets' assume the file has the format of 'first name, last name, email address'.

Here the parsing loop updated with it:

# assume no file header

# declare variables
my ($firstname, $lastname, $email);

# tell csv positions
$csv->bind_columns( \$firstname, \$lastname, \$email );

# process data
while ( my $columns = $csv->getline($fh) ) {
$email->send( $firstname, $lastname, $email);

Painfully good :)

A pretty good post/article about CSV and Perl at Parse CSV

No comments:

Post a Comment