public inbox for [email protected]
help / color / mirror / Atom feedFrom: Adrian Klaver <[email protected]>
To: Karsten Hilbert <[email protected]>
To: [email protected]
Subject: Re: iterating over DictRow
Date: Thu, 24 Sep 2020 18:00:54 -0700
Message-ID: <[email protected]> (raw)
In-Reply-To: <[email protected]>
References: <[email protected]>
<AM0PR07MB4036EA8B58205084F30DA2AC87390@AM0PR07MB4036.eurprd07.prod.outlook.com>
<[email protected]>
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-v...
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
[email protected]
view thread (12+ messages) latest in thread
reply
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients using the --to and --cc options:
reply via email
To: [email protected]
Cc: [email protected], [email protected], [email protected]
Subject: Re: iterating over DictRow
In-Reply-To: <[email protected]>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox