sphalaris has asked for the wisdom of the Perl Monks concerning the following question:
The two lines of data emanate from a MySql CSV/dump, which I'm trying to prepare for insertion into an SQLite table. (The import function on SQlitebrowser doesn't parse the dump correctly).
As you can see the CSV dump contains a quoted text field, which, unfortunately, sometimes contains the ", " separator of the CSV line.
We are told not to try to parse CSV ourselves, but to get Text::CSV to do it....but that module, as I use it, doesn't cope with this either. Have I overlooked some attribute which is supposed to deal with this? Any suggestions gratefully received.
use strict; use Text::CSV; my @lines=<DATA>; my $csv = Text::CSV->new ({sep_char => ', ' }); my @AoA; for (@lines) { if (/^\((.+?)\).?/){my $con=$1; if ($csv->parse($con)) { my @fields = $csv->fields(); push @AoA,[@fields]; } else { warn "Line could not be parsed: $_\n"; } } } $"="//";for (@AoA){print "no_fields: $#$_ : @$_\n";} __DATA__ (101, '1997-02-25', 'S1', 31.00, NULL, 0.00, 'this becomes two fields, + so no go', 5.11), (102, '1998-03-26', 'S1', 31.00, NULL, 0.00, 'this will remain one fie +ld', 6.11),
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Text::CSV - parsing
by Eily (Monsignor) on Mar 14, 2017 at 18:06 UTC | |
by haukex (Archbishop) on Mar 14, 2017 at 18:16 UTC | |
by Eily (Monsignor) on Mar 14, 2017 at 18:24 UTC | |
|
Re: Text::CSV - parsing
by haukex (Archbishop) on Mar 14, 2017 at 18:10 UTC | |
|
Re: Text::CSV - parsing
by huck (Prior) on Mar 14, 2017 at 18:07 UTC | |
|
Re: Text::CSV - parsing
by Tux (Canon) on Mar 15, 2017 at 14:01 UTC | |
|
Re: Text::CSV - parsing
by sphalaris (Novice) on Mar 14, 2017 at 18:58 UTC |