Alan> The other problem is accessing data in each row by column name. In
    Alan> Ruby I can say:

    Alan> Print row["ColName"]

    Alan> In Python; however, I must access to row contents by integer
    Alan> index, like PRINT ROW[0], which reduces my program's readability.

    Alan> Can I access to row's contents by column name?

There are a couple things you can try.  First, see if the adaptor for your
database has a way to specify that query results should be returned as a
list of dicts instead of a list of tuples.  MySQLdb allows you to select the
style of cursor class to instantiate when you create the connection.  I
think Psycopg provides a dictcursor() method on the connection (though I may
be misremembering - it's been awhile).  Other adaptors may provide similar
functionality.

Failing that, you can whip something up yourself using the description
attribute to which Fredrik referred:

    for row in curs.fetchall():
        row = dict(zip([d[0] for d in curs.description], row))
        ...

Skip
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to