DSVWizard.py

Skip Montanaro skip at pobox.com
Tue Jan 28 00:59:42 CET 2003


    Dave> Shouldn't we first come up with a project plan.  If the eventual
    Dave> goal is to get this into Python we are going to have to write a
    Dave> PEP.

I'm working on a PEP... ;-) This thread is all good grist for the mill.
I'll try to get something minimal you can throw tomatoes at tonight or
tomorrow.

    Dave> * Define Python API for CSV parser.

    Dave> * Define extension module API.

I'm not sure you need to define an extension module API.  I view the
extension module is essentially an implementation detail.

    Cliff> As far as DSV's current API, I'm not too attached to it, and I
    Cliff> think that it could be mimicked sufficiently by adding a
    Cliff> parser.parseall() method to Dave's API so the programmer would
    Cliff> have the option of getting the entire file as a list without
    Cliff> having to write a loop.

    Dave> I think that we should be prepared to go back to the drawing board
    Dave> on the API if necessary.  Once we have enough variants registered
    Dave> we will be in a better position to come up with the "right" API.

Hmmm...  I'd like to get something into 2.3 without a wholesale rewrite if
possible.  I see two basic operations:

    * suck the contents of a file-like object opened for reading into a list
      of lists (or iterable returning lists)

    * write a list of lists to to a file-like object opened for writing

I view the rest of the API as essentially just tweaks to the formatting
parameters.

I think Dave's csv module (should I be calling it Object Craft's csv module?
I don't mean to slight other contributors) is fairly close to this already,
though it would be nice to be able to read a CSV file like so:

    import csv

    csvreader = csv.parser(file("nastiness.csv"))
    # csvreader.setparams(dialect="excel2000", quote='"', delimiter='/')

    for row in csvreader:
        process(row)

and write it like so:

    import csv

    csvwriter = csv.writer(file("newnastiness.csv", "w"))
    # csvwriter.setparams(dialect="lotus123", quote='"', delimiter='/')

    for row in someiterable:
        csvwriter.write(row)

The .setparams() method can obviously be collapsed into the constructors.

I could thus implement a CSV dialect converter (do others like "dialect"
better than "variant"?) thus:

    import csv

    csvreader = csv.parser(file("nastiness.csv"), dialect="excel2000")
    csvwriter = csv.writer(file("newnastiness.csv", "w"),
                           dialect="lotus123", delimiter='/')

    for row in csvreader:
        csvwriter.write(row)

Skip



More information about the Csv mailing list