pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Adrian Klaver <adrian.klaver@aklaver.com>
To: tango ward <tangoward15@gmail.com>
To: pgsql-general@lists.postgresql.org
Subject: Re: KeyError: self._index[x]
Date: Mon, 7 May 2018 21:10:31 -0700
Message-ID: <4fed56f4-052a-8b5b-4b2a-5c108cf2e60e@aklaver.com> (raw)
In-Reply-To: <CAA6wQL+vkvVYLhHJ_zcq+w-cdJb78qwVB2KvQSxXz-T3FY0wag@mail.gmail.com>
References: <CAA6wQLJGd-c8rqAetCrk4QMAqFGaX-dqTMVAc934PptvrKqVrg@mail.gmail.com>
	<CAA6wQL+vkvVYLhHJ_zcq+w-cdJb78qwVB2KvQSxXz-T3FY0wag@mail.gmail.com>

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




view thread (9+ messages)  latest in thread

Message-ID: <4fed56f4-052a-8b5b-4b2a-5c108cf2e60e@aklaver.com>
Permalink:  ../4fed56f4-052a-8b5b-4b2a-5c108cf2e60e@aklaver.com/
Also on:    postgresql.org/message-id/4fed56f4-052a-8b5b-4b2a-5c108cf2e60e@aklaver.com

 · 

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 from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: adrian.klaver@aklaver.com, tangoward15@gmail.com
  Subject: Re: KeyError: self._index[x]
  In-Reply-To: <4fed56f4-052a-8b5b-4b2a-5c108cf2e60e@aklaver.com>
  References: <CAA6wQLJGd-c8rqAetCrk4QMAqFGaX-dqTMVAc934PptvrKqVrg@mail.gmail.com> <CAA6wQL+vkvVYLhHJ_zcq+w-cdJb78qwVB2KvQSxXz-T3FY0wag@mail.gmail.com> <4fed56f4-052a-8b5b-4b2a-5c108cf2e60e@aklaver.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox