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

Re: Parsing a .csv file

Thread Previous | Thread Next
From:
Michael Fowler
Date:
February 11, 2002 18:55
Subject:
Re: Parsing a .csv file
Message ID:
20020212025447.GA10750@shoebox.net
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.

 
Michael
--
Administrator                      www.shoebox.net
Programmer, System Administrator   www.gallanttech.com
--

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About