Skip to content
Advertisement

query from postgresql using python as dictionary

I’m using Python 2.7 and postgresql 9.1. Trying to get dictionary from query, I’ve tried the code as described here: http://wiki.postgresql.org/wiki/Using_psycopg2_with_PostgreSQL

JavaScript

It is printing the next answer:

JavaScript

printing the item itself, show me that it is list. The excepted answer was dictionary.

Edit:

Trying the next:

JavaScript

returns

JavaScript

Advertisement

Answer

If you don’t want to use a psycopg2.extras.DictCursor you can create a list of dictionaries for the results using cursor.description:

JavaScript

I use the following function fairly regularly:

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement