Tuesday, December 01, 2009

extract quoted strings with commas from csv

to extract from a csv file that may have the following format:

a,b,c,"d,e,f",g,h,i,"j,k,l"

so that the resulting array contains

a
b
c
d,e,f
g
h
i
j,k,l

the following script worked:

open (F, $file);
while (<F>) {
while(/("[^"]+")/) {
$new = $orig = $1
$new =~ s/,/__comma__/g;
s/$orig/$new/;
}
@values = map {
s/__comma__/,/g;
"\'$_\'";
} split ',';

Labels: , ,

0 Comments:

Post a Comment

<< Home