DSVWizard.py

Dave Cole djc at object-craft.com.au
Tue Jan 28 01:23:37 CET 2003


>>>>> "Skip" == Skip Montanaro <skip at pobox.com> writes:

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

Excellent.

Dave> * Define Python API for CSV parser.

Dave> * Define extension module API.

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

True.

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

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

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

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

Sounds easy :-)

Skip> I think Dave's csv module (should I be calling it Object Craft's
Skip> csv module?  I don't mean to slight other contributors)

Call it Object Craft's.  I did the initial work but Andrew has his
fingerprints all over it now.

Skip> import csv
Skip> 
Skip> csvreader = csv.parser(file("nastiness.csv"))
Skip> # csvreader.setparams(dialect="excel2000", quote='"', delimiter='/')
Skip> 
Skip> for row in csvreader:
Skip>     process(row)

That is a really nice interface.  I like it a lot.

Skip> import csv
Skip> 
Skip> csvwriter = csv.writer(file("newnastiness.csv", "w"))
Skip> # csvwriter.setparams(dialect="lotus123", quote='"', delimiter='/')
Skip> 
Skip> for row in someiterable:
Skip>     csvwriter.write(row)

Very nice.

Skip> The .setparams() method can obviously be collapsed into the
Skip> constructors.
Skip> 
Skip> I could thus implement a CSV dialect converter (do others like
Skip> "dialect" better than "variant"?) thus:
Skip> 
Skip>     import csv
Skip> 
Skip>     csvreader = csv.parser(file("nastiness.csv"), dialect="excel2000")
Skip>     csvwriter = csv.writer(file("newnastiness.csv", "w"),
Skip>                            dialect="lotus123", delimiter='/')
Skip> 
Skip>     for row in csvreader:
Skip>         csvwriter.write(row)

This is excellent stuff.  I am not very good at naming, but "dialect"
looks good to me.

- Dave

-- 
http://www.object-craft.com.au




More information about the Csv mailing list