Search code examples
pythonsqlpsycopg2

Trying to convert psycopg2 -> Python data from list tuples to dict


I have a postgreSQL table called stockdata with financial statement information for about 250 companies with the following setup:

Company  q4_2022_revenue q3_2022_revenue
CPI Card 126436000       124577000  
Zuora    103041000       101072000 
…

I used the psycopg2 package in Python to import this data into a ‘main.py’ file with the following code:

import psycopg2

conn=psycopg2.connect(‘dbname=postgres user=postgres password=‘…’)
cur=conn.cursor()
cur.execute(‘SELECT company, (q4_2022_revenue-q3_2022_revenue)/CAST(q3_2022_revenue AS float) AS revenue_growth FROM stockdata ORDER BY revenue_growth DESC LIMIT 25;
records=cur.fetchall()
print(records)

Which gave me the following results:

[(‘Alico’, 9.269641125121241),(‘Arrowhead Pharmaceuticals’, 1.7705869324473975),…],

where records[0] is equal to (‘Alico’, 9.269641125121241) and the type for records as a whole was given as list.

I’m trying to figure out how I can assign a variable separately to both the company name and change in revenue so I can modify the revenue change from, for example, 9.2696 to 926.96% (the company is in a seasonal industry - agriculture - and is recovering from a hurricane so the data makes sense). The revenue change would be multiplied by 100 and given a % sign on the end and rounded to 2 decimal digits.

I tried adding the line:

list((x,y) for x,y in records)

with the intention of assigning x as key values for company name and y as pair values for revenue change, but I received a ‘name ‘x’ is not defined’ error when calling records[x].

How can I convert my psycopg2 list of tuples to a mutable dict and assign the results to a key-value pair so I can modify the formatting of the values?

EDIT: Typo in 926.96%.


Solution

  • In psycopg2.extras there is the RealDictCursor which can output query results as dicts.

    Change your cursor to use it as below:

    conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor)