SPUG: CSV module that can handle multi-line entries?
William Julien
moonbeam at catmanor.com
Wed Jan 18 16:36:20 PST 2006
Try this...
#!/usr/bin/perl -w
#
$text= ',A,cool "cat",A B,"A,B","A,""B"","A,\""B","A,\\\
\""B",,B,"foo","this
entry spans two lines"" and has an embedded quote",bar,snark';
print "Original text\n\n",$text . "\n\n";
@fields = ();
push(@fields, defined($+)?scalar($_=$+,$_ =~ s/""/"/g,$_):undef)
while $text =~ m{
"([^\"]*(?:\"\"[^\"]*)*)",? ## standard string, w/
possible comma
| ([^,]+),? ## anything else, w/ possible
comma
| , ## lone comma
}gx;
## final empty field for trailing comma
push(@fields, undef) if substr($text, -1, 1) eq ',';
print "csv deliminated text\n\n";
printf "%6s%12s\n","field","data\n";
for ($i = $[; $i <= $#fields; $i++) {
printf "%6s %12s", $i . " |" ,
(defined($fields[$i])?$fields[$i]:"undefined") . "|\n";
}
On Jan 17, 2006, at 4:10 PM, James Moore wrote:
> "foo","this
> entry spans two lines"" and has an embedded quote",bar,snark
More information about the spug-list
mailing list