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
|