Get the result in dict format with Python psycopg2

In the implementation that connects to the DB (Postgresql) of the Heroku application with Python It was very troublesome for debugging that the result was obtained as an array and the key value could not be determined.

When I looked it up, the following answer was good

http://stackoverflow.com/questions/21158033/query-from-postgresql-using-python-as-dictionary

get_dict_resultset.py


#!/var/bin/python 
import psycopg2
import psycopg2.extras

def get_dict_resultset(sql):
    conn = psycopg2.connect("dbname=pem host=localhost user=postgres password=Drupal#1008")
    cur = conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
    cur.execute (sql)
    results = cur.fetchall()
    dict_result = []
    for row in results:
        dict_result.append(dict(row))
    return dict_result

sql = """select * from tablename"""
return get_dict_resultset(sql)

If you use this function, you can easily get the SQL execution result in dict format.

Recommended Posts

Get the result in dict format with Python psycopg2
[Python] Get the files in a folder with Python
[Python] Get the numbers in the graph image with OCR
Get the desktop path in Python
Get the weather with Python requests
Get the weather with Python requests 2
Get the script path in Python
Get the desktop path in Python
Get the host name in Python
Get started with Python in Blender
Get the MIME type in Python and determine the file format
Get the value while specifying the default value from dict in Python
Get additional data in LDAP with python
How is the progress? Let's get on with the boom ?? in Python
[Python] Get the variable name with str
Display Python 3 in the browser with MAMP
The result of installing python in Anaconda
Get Started with TopCoder in Python (2020 Edition)
format in python
Get the EDINET code list in Python
How to get the date and time difference in seconds with python
Get and convert the current time in the system local timezone with python
Get the weather in Osaka via WebAPI (python)
Load the network modeled with Rhinoceros in Python ③
Get the caller of a function in Python
View the result of geometry processing in Python
Calculation result after the decimal point in Python
[Automation] Extract the table in PDF with Python
Get the X Window System window title in Python
Load the network modeled with Rhinoceros in Python ②
How to get the files in the [Python] folder
Load the network modeled with Rhinoceros in Python ①
Image format in Python
Get date in Python
Get date with python
Get information on the 100 most influential tech Twitter users in the world with python.
How to get a list of files in the same directory with python
How to get the variable name itself in python
How to get the number of digits in Python
Get standard output in real time with Python subprocess
Crawl the URL contained in the twitter tweet with python
Convert the image in .zip to PDF with Python
Read the linked list in csv format with graph-tool
Write letters in the card illustration with OpenCV python
Get the size (number of elements) of UnionFind in Python
Get the value selected in Selenium Python VBA pull-down
Output log in JSON format with Python standard logging
Find this week's date in any format with python
Get the operation status of JR West with Python
Get the URL of the HTTP redirect destination in Python
Get in touch with functional programming in JavaScript or Python 3
Get the value of a specific key in a list from the dictionary type in the list with Python
[For beginners] Web scraping with Python "Access the URL in the page to get the contents"
It is easy to execute SQL with Python and output the result in Excel
Get YouTube Comments in Python
Get country code with python
Easily format JSON in Python
Scraping with selenium in Python
Get last month in python
Working with LibreOffice in Python
Download the file in Python