[DB-SIG] May we get an actual ANSWER to the F.A.Q...How can I
construct a dictionary???
Chris Cogdon
chris at cogdon.org
Mon Sep 22 17:41:21 EDT 2003
On Monday, Sep 22, 2003, at 14:22 US/Pacific, Vernon Cole wrote:
> I have been away from the leading edge for a long time, and am trying
> to
> jump back
> in and learn the new stuff. Python seems like a GREAT tool.
> But.....
>
> From:
> ...PEP: 249
> ...Title: Python Database API Specification v2.0
> ...Version: $Revision: 1.9 $
> I extract the following:
> vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
> Question:
>
> How can I construct a dictionary out of the tuples returned by
> .fetchxxx():
>
> Answer:
>
> There are several existing tools available which provide
> helpers for this task. Most of them use the approach of using
> the column names defined in the cursor attribute .description
> as basis for the keys in the row dictionary.
> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> Nice to hear that there are "several existing tools".
>
> I have been searching for a week now, but cannot find any of them.
>
> Would someone be so kind as to actually publish a LIST of such tools??
>
> I find it difficult to believe that the "latest and greatest" tools
> available
> in the 21st century still cannot return a value from a named field
> (column)
> in one row of a database. Good grief! How can I NOT have a feature on
> a 256
> MB Pentium in 2003 that I had
> on a 30 KB PDP-11 in 1983?
> Was RDM _that_ far ahead of its time?
cursor = db.cursor ()
cursor. execute ( 'some query' )
a_row = cursor.fetchone ()
d = {}
for descr, val in zip ( cur.description, a_row ):
d[descr[0]] = val
# dictionary 'd' now has the answers you want
# the format of 'description' may vary from database type to type,
unfortunately, but the above
# works for pyPgSQL
del sarcasm # :)
--
("`-/")_.-'"``-._ Chris Cogdon <chris at cogdon.org>
. . `; -._ )-;-,_`)
(v_,)' _ )`-.\ ``-'
_.- _..-_/ / ((.'
((,.-' ((,/ fL
More information about the DB-SIG
mailing list