parse CSV review
From: Jeff Thies (jeff_at_spamalanadingong.com)
Date: 07/19/04
- Next message: Anno Siegel: "Re: HoHoH (hash of HoH's)"
- Previous message: Gunnar Hjalmarsson: "Re: double eval?"
- Next in thread: Brad Baxter: "Re: parse CSV review"
- Reply: Brad Baxter: "Re: parse CSV review"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Mon, 19 Jul 2004 07:08:10 GMT
I have nothing against Text::CSV except that it can be a pain
coercing recalcitrant sysadmins to install modules. This can take days
to never.
A while back I asked about glueing CSV records back together that
contain linebreaks. The crux of this is that valid records contain even
numbers of double quotes.
It's occured to me that a simlar strategy would work for CSV fields.
sub parseCSVLine{
my $line=shift;
my @fields=();my $field=''; # initialize
my @fragments=split(/,/,$line,-1); # split into "," seperated fragments
foreach my $nibble(@fragments){
if($field){$field .= ','} # add the missing commas;
$field .= $nibble; # combine fragment into a field until...
my $count = $field =~tr/"/"/; # count quotes
unless($count % 2){ # ...there's an even number of double quotes
$field =~s/""/"/g; $field=~s/^\s*"//g; $field=~s/"\s*$//g; # fix quotes
# fix quotes
push @fields, $field;
$field=''; # reinitialize $field
}
}
return @fields;
}
It all seems too easy. Does this seem like a reasonable approach?
Have I missed something? I'm unsure of that fix quotes line.
BTW that wild parse CSV regex that's floating around doesn't seem to
work on my data. It's a bit too complex for me to figure out why not!
Jeff
- Next message: Anno Siegel: "Re: HoHoH (hash of HoH's)"
- Previous message: Gunnar Hjalmarsson: "Re: double eval?"
- Next in thread: Brad Baxter: "Re: parse CSV review"
- Reply: Brad Baxter: "Re: parse CSV review"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]