Re: iterating over DictRow

From: Adrian Klaver <adrian(dot)klaver(at)aklaver(dot)com>
To: Karsten Hilbert <Karsten(dot)Hilbert(at)gmx(dot)net>, psycopg(at)lists(dot)postgresql(dot)org
Subject: Re: iterating over DictRow
Date: 2020-09-25 01:00:54
Message-ID: 9f9f7536-9d5f-d95b-b1eb-eed7ec1b83f6@aklaver.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: psycopg

On 9/24/20 2:11 PM, Karsten Hilbert wrote:
> On Thu, Sep 24, 2020 at 02:53:40PM +0000, David Raymond wrote:
>
>> Since DictRow's can be indexed by either the field name or
>> the field index (either record["ID"] or record[0]) then I
>> think what it "should" be is a little ambiguous.
>
> I eventually thought so. I was, however, wondering whether I
> should have _expected_ iteration over a DictRow to be a list
> iteration.

Maybe. The issue is as below. Thanks to Maurice Meyer's answer to this
SO question:

https://stackoverflow.com/questions/63200437/psycopg2-extras-dictrow-behaves-differently-using-for-vs-next/63200557#63200557

for pointing me in the right direction.

https://github.com/psycopg/psycopg2/blob/fbba461052ae6ebc43167ab69ad91cadb7914c83/lib/extras.py

class DictRow(list):

...

def __getitem__(self, x):
if not isinstance(x, (int, slice)):
x = self._index[x]
return super(DictRow, self).__getitem__(x)

So if the value passed to __getitem__() is a integer or slice it does a
list index.

So:

con = psycopg2.connect("dbname=production host=localhost user=postgres",
cursor_factory=DictCursor)
cur = con.cursor()
cur.execute("select * from cell_per")

rs = cur.fetchall()

type(rs)

list

r0 = rs[0]

type(r0)

psycopg2.extras.DictRow

for fld in r0.__iter__():
print(fld)

5
H PREM 3.5
18
None
2004-06-02 15:11:26
None
postgres
herb
none
HP3

What you where trying:

for fld in r0:
print(r0[fld])

KeyError: 'H PREM 3.5'

So the 5 works because there are at least 6 items in the record. The 'H
PREM 3.5' fails because it is a value not a key.

cur.execute("select cell_per from cell_per")
rs = cur.fetchall()
r0 = rs[0]

r0
[18]

for fld in r0:
print(r0[fld])

IndexError: list index out of range

Fails as there are not 19 items in the DictRow.

>
> Either of those
>
>> for field in dict(the_dict):
>> for field in the_dict.keys():
>
> work, however (or RealDictCursor, for that matter).
>
> Thanks all,
> Karsten
> --
> GPG 40BE 5B0E C98E 1713 AFA6 5BC0 3BEA AC80 7D4F C89B
>
>

--
Adrian Klaver
adrian(dot)klaver(at)aklaver(dot)com

In response to

Responses

Browse psycopg by date

  From Date Subject
Next Message Karsten Hilbert 2020-09-25 07:48:20 Re: iterating over DictRow
Previous Message Karsten Hilbert 2020-09-24 21:11:47 Re: iterating over DictRow