Is anyone happy with csv module?

John Machin sjmachin at lexicon.net
Tue Dec 11 17:27:21 EST 2007


On Dec 12, 8:49 am, "massimo s." <deviceran... at gmail.com> wrote:
> On 11 Dic, 22:37, John Machin <sjmac... at lexicon.net> wrote:
>
> > On Dec 12, 6:14 am, "massimo s." <deviceran... at gmail.com> wrote:
>
> > > Hi,
>
> > > I'm struggling to use the python in-built csv module, and I must say
> > > I'm less than satisfied. Apart from being rather poorly documented,
>
> > Patches are welcome :-)
>
> Yes, but maybe I was in the wrong. I'm not so bold to submit patches
> to an official Python module without asking.
> *I* feel troubles, but maybe it's just me being dense.

Quite probably. You did however assert that it was poorly documented.

> > > I
> > > find it especially cumbersome to use,
>
> > Can you be more specific? What are you trying to do with it?
>
> See examples in previous post.

Definitions of cumbersome vary with the definer. IMHO the dictionary
interface in the csv module is an unnecessary gimmick. What you want
to do can be done easily using the bare-bones interface. If you are
doing that sort of thing often, it can be packaged up into a function.

# untested
# Allows for non-uniform column lengths.
maxlen = max(len(value) for value in adict.itervalues())
# The order in which columns appear is
# not easily predictable.
keys = adict.keys()
# ... or supply keys as an argument to the function.
writer.writerow(keys)
for rowx in xrange(maxlen):
    row = []
    for key in keys:
        try:
            value = adict[key][rowx]
        except IndexError:
            value = ''
        row.append(value)
    writer.writerow(row)


>
> > > and also rather limited.
>
> > What extra facilities do you think there should be?
>
> Ability to work by columns together with rows and maybe some random
> access facilities would be nice. A more user-friendly interface too.

Your prospectus would have to be much less vague and woolly for anyone
to pay you much attention.

>
> > A CSV file is organised such that each line of the file represents a
> > row, and the nth field in the line relates to the nth column, so it's
> > natural for any CSV reader/writer to work by rows.
>
> Yes, but it's natural for a spreadsheet-like thing to have organized
> columns of data, often.
> Often I want those columns to be read into lists, or to write lists
> into columns. The actual csv doesn't allow this naturally. Especially
> writing is a bit painful.
>
> I just wanted to know if there was something allowing this with a
> simple command, that I missed, or if just there wasn't.

If you can't find in the documentation, treat it as not existing.

>
> > Accessing the data by columns *instead* of by rows would definitely
> > not be appreciated by people who are using the csv module to read
> > millions of lines of data.
>
> I don't want anything *instead*, I would like *additional*. :)

You did say "working by *rows* instead than by *columns*"

> (Btw: who is using csv to read >10**6 lines of data?)

The folk who wrote what became the csv module. Me. Anybody who gets
query results from a big database and doesn't want the overhead of
(say) XML.

>
> > > So I have some questions:
> > > 1) Is there a good tutorial, example collection etc. on the csv module
> > > that I'm missing?
>
> > AFAIK, no.
>
> Ok. I found something on google but nothing answering to my questions.
>
> > > 2) Is there an alternative csv read/write module?
>
> > Is your googler broken?
>
> Apparently, yes. I googled but apart from some hint here and there of
> someone thinking about writing a pure Python csv module, I found
> nothing. I'm usually decent at googling, but maybe my skills are
> wearing out.
>
> > > 3) In case anyone else is as unhappy as me, and no tutorial etc.
> > > enlighten us, and no alternative is present, anyone is interested in
> > > an alternative csv module?
>
> > -1
>
> Ok. :)
>
> > > I'd like to write one if it is the case.
>
> > In what language?
>
> Python.

It wouldn't run fast enough (without psyco).



More information about the Python-list mailing list