develooper Front page | perl.beginners | Postings from February 2002

Re: Parsing a .csv file

Thread Previous | Thread Next
Michael Fowler
February 11, 2002 18:55
Re: Parsing a .csv file
Message ID:
On Mon, Feb 11, 2002 at 09:23:09PM -0500, Steven Arbitman wrote:
> I know parsing a comma-separated value file should be easy:
> @array = split /,/;  # just split the line on commas
> However, my input csv file looks like this:
> Name,"street,city,state,zip",phone,email,"comments, may have commas, 2"
> Note, not all fields have quotes, only those which contain commas have
> quotes.

Parsing CSV formats, especially considering some of the hacks put in place
to quote commas and quote quotes, can become fairly non-trivial, and easy to
mess up.  I would suggest using Text::CSV, Text::CSV_XS, or DBD::CSV, all of
which are available from CPAN.

Programmer, System Administrator

Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About