tuple2csv snipplet
Dave Cole
djc at object-craft.com.au
Wed Jul 4 06:35:38 EDT 2001
>>>>> "Shakeeb" == Shakeeb Alireza <sa at bayt.net> writes:
Shakeeb> Hi folks, A friend and I have just spent a bit of time trying
Shakeeb> to convert tuples (generated from an SQL query) into comma
Shakeeb> separated values. As it seems to work we thought we'd share
Shakeeb> what we have come up with so far. It's a bit of a hack
Shakeeb> however... is there a better way to do this?
Go to our web page and check out the CSV module:
http://www.object-craft.com.au/projects/csv/
It is not immediately obvious but the parser object can be used to
convert sequences to a CSV record.
% python
Python 2.0 (#0, Apr 14 2001, 21:24:22)
[GCC 2.95.3 20010219 (prerelease)] on linux2
Type "copyright", "credits" or "license" for more information.
>>> import csv
>>> t1 = (None, 'spam', 'bar', "Python's kewl", None, None)
>>> t2 = (123, 'She said "Ni!" and smiled.')
>>> p = csv.parser()
>>> print p.join(t1)
None,spam,bar,"Python's kewl",None,None
>>> print p.join(t2)
123,"She said ""Ni!"" and smiled."
>>>
It is really fast - so if you are dealing with a large amount of data
it is the best option I can think of.
- Dave
--
http://www.object-craft.com.au
More information about the Python-list
mailing list