Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kLc6o-0000fx-Kd for psycopg@arkaria.postgresql.org; Fri, 25 Sep 2020 01:01:11 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kLc6m-0007Z3-Hf for psycopg@arkaria.postgresql.org; Fri, 25 Sep 2020 01:01:08 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kLc6k-0007Yw-Mk for psycopg@lists.postgresql.org; Fri, 25 Sep 2020 01:01:08 +0000 Received: from out2-smtp.messagingengine.com ([66.111.4.26]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kLc6c-0001d9-SS for psycopg@lists.postgresql.org; Fri, 25 Sep 2020 01:01:06 +0000 Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailout.nyi.internal (Postfix) with ESMTP id DFB945C01B4; Thu, 24 Sep 2020 21:00:56 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute4.internal (MEProxy); Thu, 24 Sep 2020 21:00:56 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= subject:to:references:from:message-id:date:mime-version :in-reply-to:content-type:content-transfer-encoding; s=fm1; bh=W sUlTxqUkHMrUSztbKv0w0DTKDajI8wc2wf/T4E2Xrc=; b=WwMMORltvW15d1A73 9dAyovm0K+D8+DzBIHOHQ9DmLzqyBKedjmO2zkb4483x/InDiG4zpuKgkpzuGrKh dDBpk90OMc1A6KGvFGOxpROfvfEPoVHK3BzHRSO87HVY0lqiOY+uPgjOAz+CItxV BshrBTbcVmQ1+607U+KRcXeBGchH1jGlmIQUvTFPTd6uQNRaPXMKU2/Iak537hAS Se3fm0Z3aUxOBzuDN0X8HfmhzaK20mvsScyZOk96ETAi/gxvH6mCK/AEmdbp0EYQ UU2JEeK1WKZLFVwDzob+7Is/W581B3CcWHkAgx9CN+QTMQRIFpPr2QvbDrfCugRf bvX8A== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender :x-sasl-enc; s=fm3; bh=WsUlTxqUkHMrUSztbKv0w0DTKDajI8wc2wf/T4E2X rc=; b=NhF09M+NlHYjG93v8GINSmOi5FNHGiiCNw+xHKIqsuKgLbrZuj30e9XTE G9sNKs/yAmxGrx7HbXzJn2HKwETgiC9jiqJsWUH3N8ZJaHBUGruRELFtLjWdtiiK /vhBmULFk9XoXrbJ4VUQWN+CVRxly0CeKVveiibZhhJQQSWZWwKZWi8Xk7LkQeM+ AFaBGZngqdts+Dy2YRmHyMafSHhphU0aFb/NxxXOAX7AQ9ejuvEjx9OAnlqtmbES NpsZ+vnzKar8l04IMBC1kEoAr/xSaGK9eO+1qywMuhaADNRSZuztXWRAm9uhSofs QuLE66VYEFEoleuqR3WRL5WGYbYPQ== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedujedrudelgdefhecutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfghnecu uegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenuc fjughrpefuvfhfhffkffgfgggjtgfgsehtjeertddtfeejnecuhfhrohhmpeetughrihgr nhcumfhlrghvvghruceorggurhhirghnrdhklhgrvhgvrhesrghklhgrvhgvrhdrtghomh eqnecuggftrfgrthhtvghrnhepgedttddtteeutddujeegkefgjeduueevfeeileeiffdu hefhudfgffevhfegvefgnecuffhomhgrihhnpehsthgrtghkohhvvghrfhhlohifrdgtoh hmpdhgihhthhhusgdrtghomhenucfkphepjeehrddujedvrdehrddvfedunecuvehluhhs thgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomheprggurhhirghnrdhklh grvhgvrhesrghklhgrvhgvrhdrtghomh X-ME-Proxy: Received: from [192.168.1.3] (75-172-5-231.tukw.qwest.net [75.172.5.231]) by mail.messagingengine.com (Postfix) with ESMTPA id 1D48A3064610; Thu, 24 Sep 2020 21:00:56 -0400 (EDT) Subject: Re: iterating over DictRow To: Karsten Hilbert , psycopg@lists.postgresql.org References: <20200923215444.GA25406@hermes.hilbert.loc> <20200924211147.GA2089@hermes.hilbert.loc> From: Adrian Klaver Message-ID: <9f9f7536-9d5f-d95b-b1eb-eed7ec1b83f6@aklaver.com> Date: Thu, 24 Sep 2020 18:00:54 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.12.0 MIME-Version: 1.0 In-Reply-To: <20200924211147.GA2089@hermes.hilbert.loc> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk 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.klaver@aklaver.com