PEP 305 - CSV File API

Paul Rubin phr-n2003b at NOSPAMnightsong.com
Sun Feb 2 03:32:18 CET 2003


Tyler Eaves <tyler at cg1.org> writes:
> > Without commenting on the API itself, I will say that it is a very good 
> > idea to have a module like that in the standard library. It is used *so* 
> > often.
> 
> Yea, I see this being implemented more as a module (If even that...)
> 
> This stuff is really pretty trivial.
> 
> For what I need, the following is typical:
> 
> import string
> 
> inf = open('somefile.txt','r')
> lines = inf.readlines()
> inf.close()
> data = []
> for l in lines:
>     data.append(string.split(l,sep))

I think it's good to have a library module.  Your example, for example,
doesn't handle quoted fields:

    Jane Smith, 9 Elm Street
    Fred Jones, 1313 Mockingbird Lane
    "Thurston B. Howell, III", "Uncharted Desert Isle"

With a comma separator, your example will get confused on the third
line.  It will also fail to strip the quotation marks from either field.

Implementing CSV correctly and generally involves getting a lot of
little details like that right.  And if you code it in pure Python,
by the time you handle all those cases, you get pretty poor performance.

So it's better to code it in C and put it in the library once and for all.





More information about the Python-list mailing list