Trivial Thoughts
Thoughts and discussion on programming projects using the Python language.


Python Sites of Note
Software Development



Recent Posts
 5/5/04
 5/5/04
 4/23/04
 9/23/03
 9/22/03
 9/12/03
 9/11/03
 8/21/03
 7/21/03
 7/17/03
 7/10/03
 7/7/03
 7/1/03
 6/26/03
 6/25/03
 6/18/03
 6/15/03
 6/2/03
 5/28/03


Subscribe to "Trivial Thoughts" in Radio UserLand.

Click to see the XML version of this web page.

Click here to send an email to the editor of this weblog.
 

 

Friday, September 12, 2003
 

Python 2.3 and the csv module

[Edited on 05/06/04 to fix the class name, and add the write method]

Another new module we get with Python 2.3 that I've been itching to get my hands on is the csv module, which gives us a general-purpose way of parsing 'comma separated values' data created by a wide range of applications.  While I don't have a need for that kind of capability in the current project being developed here, I do need it for other projects I work on.

Once I actually started working with the csv module, I quickly discovered to my surprise just how limited its API is.  Basically, if what you want it to iterate over lines in a file from start to end, parsing out and returning csv data, it's easy.  But if, like me, you need to randomly seek around in a fixed-record-length file of csv data, reading in a record and then parsing that record string...

The csv readers provided by the module take a file-like object that follows the iterator protocol and supplies a next method.  Period.  There is no provision for parsing a string.  To me, parsing a string containing csv data is such a common use case for a csv parser that I'm astounded it's not provided.

That doesn't mean you can't parse a csv string.  You just have to write an adaptor, heehee.  Like so:

import sys
import csv
class StringCSVAdaptor:
    """This adaptor let's us use a string where the csv parser wants
    a file-like iterable object."""
    def __init__(self):
        self.data = ""
        return
    def __iter__(self):
        return self
   
    def next(self):
        return self.data
    def write(self, data):
        self.data = data
def main():
    # Create our adaptor object.
    adaptor = StringCSVAdaptor()
    # Create our csv parser object.
    csvReader = csv.reader(adaptor)
    # Set the adaptor's data to the string we want to parse.
    adaptor.data = 'now,is,"the, time",,for'
    # Parse our string of csv data.
    parsedData = csvReader.next()
    print "data is", parsedData
    return 0
if __name__ == "__main__":
    sys.exit(main())

So, we can get where we want.  I just wish we didn't have to jump over the barrel to get there.

BTW, as I was writing this up, I discovered that Hans Nowak over at Tao of the Machine has just posted an article, where he details his problems with the limited API of the csv module.  So I'm not the only one...


11:24:26 AM  comment []    


Click here to visit the Radio UserLand website. © Copyright 2004 Michael Kent.
Last update: 5/7/2004; 3:43:25 PM.
This theme is based on the SoundWaves (blue) Manila theme.
September 2003
Sun Mon Tue Wed Thu Fri Sat
  1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30        
Aug   Apr

Previous/Next