Re: KeyError: self._index[x] - Mailing list pgsql-general

From tango ward
Subject Re: KeyError: self._index[x]
Date
Msg-id CAA6wQLKQjPXcYgbP=jVfrSxU3eXQoKL0PXVGy7Hs58MB4=+KhQ@mail.gmail.com
Whole thread Raw
In response to Re: KeyError: self._index[x]  (Adrian Klaver <adrian.klaver@aklaver.com>)
Responses Re: KeyError: self._index[x]
List pgsql-general
Shall I loop using the 'translate' as key to my row?

On Tue, May 8, 2018 at 12:10 PM, Adrian Klaver <adrian.klaver@aklaver.com> wrote:
On 05/07/2018 08:11 PM, tango ward wrote:
Sorry, accidentally pressed send.


cur_p = conn_pr(cursor_factory=psycopg2.extras.DictCursor)
cur_t = conn_t.cursor(cursor_factory=psycopg2.extras.DictCursor)


cur_t.execute("""
                 SELECT TRANSLATE(snumber, ' ', '')
                 FROM sprofile """)


DictCursor is a hybrid dict/sequence.

# This will result in KeyError
for row in cur_t:
print row['snumber']

Above you are using it as a dict and as David pointed you would need to use translate as the key:

test=> select translate('test', '', '');
 translate
-----------
 test



# This works fine
for row in cur_t:
print row[0]

Above you are using as a sequence, so the indexing works.



Sorry again.

I would really appreciate any suggestions.

Thanks,
J



--
Adrian Klaver
adrian.klaver@aklaver.com

pgsql-general by date:

Previous
From: Adrian Klaver
Date:
Subject: Re: KeyError: self._index[x]
Next
From: tango ward
Date:
Subject: Re: KeyError: self._index[x]