[Tutor] Wanted: module to parse out a CSV line

Terry Carroll carroll@tjc.com
Wed Dec 11 03:43:03 2002


I'm writing one of my first Python apps (I've used perl up  until
now) and need to parse out lines of comma-separated values (CSV).  I'm on
a Windows/XP system.

For example, if a line contains this:

A, 232, "Title", "Smith, Adam" "1, 2, 3, 4"

I'd like to parse this into five fields:

 A
 232
 Title
 Smith, Adam
 1, 2, 3, 4

If this were perl, I'd use Text::CSV, from
<http://search.cpan.org/author/ALANCITT/Text-CSV-0.01/CSV.pm>, which works
just great.

I've found some Python CSV support, but nothing that will work for me:

 1. ASV, from <http://tratt.net/laurie/python/asv/>
    Nice, but it reads in an entire file that is assumed to be
    CSV-formatted.  That's not my case, I have a single variable I need
    to parse out (yeah, it comes from a file, but not all lines in the
    file are CSV).

 2. A CSV module from
    <http://www.object-craft.com.au/projects/csv/documentation.html>
    Perfect!  Exactly what I need.  Except the install fails looking for a
    program named cl.exe; I think it's a compiler, which I don't have.

 3. Python-DSV, at <http://python-dsv.sourceforge.net/>
    This looks like some whole separate program, rather than something
    that I can just call in to parse out a single line.  It also looks
    like it goes after a whole file at once.  Hard to tell -- there's no
    docs, unless (I presume) I install it.

This can't be a problem unique to me.  Anyone have an answer?

-- 
Terry Carroll        |
Santa Clara, CA      |   "The parties are advised to chill."
carroll@tjc.com      |       - Mattel, Inc. v. MCA Records, Inc.,
Modell delendus est  |         no. 98-56577 (9th Cir. July 24, 2002)