[Csv] Re: First Cut at CSV PEP
Dave Cole
djc at object-craft.com.au
Thu Jan 30 15:18:50 CET 2003
>>>>> "Kevin" == Kevin Altis <altis at semi-retired.com> writes:
>> Exceptions should be raised for anything else, even None. An empty
>> field is "".
[snip]
>> I really still dislike this whole None thing. Whose use case is
>> that anyway?
[snip]
Kevin> Are you saying that you want to throw an exception instead?
Kevin> Booleans may also present a problem. I was mostly thinking in
Kevin> terms of importing and exporting data from embedded databases
Kevin> like MetaKit, my own list of dictionaries (flatfile stuff),
Kevin> PySQLite, Gadfly. Anyway, the implication might be that it is
Kevin> necessary for the user to sanitize data as part of the export
Kevin> operation too. Have to ponder that.
The penny finally dropped!!!
The None thing and the implicit __str__ conversion is there in the
Object Craft parser to be compatible with the DB-API. Consider the
following code (which is close to something I had to do a couple of
years ago):
import csv
import Sybase
db = Sybase.connect(server, user, passwd, database)
c = db.cursor()
c.execute('select some stuff from the database')
p = csv.parser()
fp = open('results.csv', 'w')
for row in c.fetchall():
fp.write(p.join(row))
fp.write('\n')
We would be doing it slightly better now:
import csv
import Sybase
db = Sybase.connect(server, user, passwd, database)
c = db.cursor()
c.execute('select some stuff from the database')
csvwriter = csv.writer(file('results.csv', 'w'))
for row in c.fetchall():
csvwriter.write(row)
Or even:
import csv
import Sybase
db = Sybase.connect(server, user, passwd, database)
c = db.cursor()
c.execute('select some stuff from the database')
csvwriter = csv.writer(file('results.csv', 'w'))
csvwriter.writelines(c)
Now without the implicit __str__ and conversion of None to '' we would
require a shirtload of code to do the same thing, only it would be as
slow as a slug on valium.
- Dave
--
http://www.object-craft.com.au
More information about the Csv
mailing list