pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
KeyError: self._index[x]
9+ messages / 3 participants
[nested] [flat]

* KeyError: self._index[x]
@ 2018-05-08 03:07  tango ward <tangoward15@gmail.com>
  0 siblings, 1 reply; 9+ messages in thread

From: tango ward @ 2018-05-08 03:07 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org

Good day,

Apologies for asking again.

I am trying to remove the whitespace on student number by using TRANSLATE()
inside the execute() of psycopg2. Problem that I am getting is, even if I
will just print the row with the column name(e.g. row['snumber']), I am
getting KeyError error message. The code works if i will print the row with
index value(e.g. row[0])

`
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 """)

for row in cur_t:

`

^ permalink  raw  reply  [nested|flat] 9+ messages in thread

* Re: KeyError: self._index[x]
@ 2018-05-08 03:11  tango ward <tangoward15@gmail.com>
  parent: tango ward <tangoward15@gmail.com>
  0 siblings, 2 replies; 9+ messages in thread

From: tango ward @ 2018-05-08 03:11 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org

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 """)

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

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

Sorry again.

I would really appreciate any suggestions.

Thanks,
J

^ permalink  raw  reply  [nested|flat] 9+ messages in thread

* Re: KeyError: self._index[x]
@ 2018-05-08 03:50  David G. Johnston <david.g.johnston@gmail.com>
  parent: tango ward <tangoward15@gmail.com>
  1 sibling, 1 reply; 9+ messages in thread

From: David G. Johnston @ 2018-05-08 03:50 UTC (permalink / raw)
  To: tango ward <tangoward15@gmail.com>; +Cc: pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>

On Monday, May 7, 2018, tango ward <tangoward15@gmail.com> wrote:

>
> cur_t.execute("""
>                 SELECT TRANSLATE(snumber, ' ', '')
>                 FROM sprofile """)
>
> # This will result in KeyError
> for row in cur_t:
> print row['snumber']
>
> # This works fine
> for row in cur_t:
> print row[0]
>

So apparently when you execute your query the result has at least one
column but that column isn't named "snumber".  I'm sure there is a way in
Python to debug "row" and find out what names it does have.  Or maybe
execute the query in something like psql and observe e column name there.

That said, by default the name of columns whose values are derived by a
single function call should be the name of the function.  So "translate",
not "snumber" - the latter being consumed by the function.  You can as use
"as <alias>" to give it a different fixed name and refer to that.

David J.

^ permalink  raw  reply  [nested|flat] 9+ messages in thread

* Re: KeyError: self._index[x]
@ 2018-05-08 04:06  Adrian Klaver <adrian.klaver@aklaver.com>
  parent: David G. Johnston <david.g.johnston@gmail.com>
  0 siblings, 0 replies; 9+ messages in thread

From: Adrian Klaver @ 2018-05-08 04:06 UTC (permalink / raw)
  To: David G. Johnston <david.g.johnston@gmail.com>; tango ward <tangoward15@gmail.com>; +Cc: pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>

On 05/07/2018 08:50 PM, David G. Johnston wrote:
> On Monday, May 7, 2018, tango ward <tangoward15@gmail.com 
> <mailto:tangoward15@gmail.com>> wrote:
> 
> 
>     cur_t.execute("""
>                      SELECT TRANSLATE(snumber, ' ', '')
>                      FROM sprofile """)
> 
>     # This will result in KeyError
>     for row in cur_t:
>     print row['snumber']
> 
>     # This works fine
>     for row in cur_t:
>     print row[0]
> 
> 
> So apparently when you execute your query the result has at least one 
> column but that column isn't named "snumber".  I'm sure there is a way 
> in Python to debug "row" and find out what names it does have.  Or maybe 

Python 3+
print(row)

Python 2.7
print row

> execute the query in something like psql and observe e column name there.
> 
> That said, by default the name of columns whose values are derived by a 
> single function call should be the name of the function.  So 
> "translate", not "snumber" - the latter being consumed by the function.  
> You can as use "as <alias>" to give it a different fixed name and refer 
> to that.
> 
> David J.


-- 
Adrian Klaver
adrian.klaver@aklaver.com




^ permalink  raw  reply  [nested|flat] 9+ messages in thread

* Re: KeyError: self._index[x]
@ 2018-05-08 04:10  Adrian Klaver <adrian.klaver@aklaver.com>
  parent: tango ward <tangoward15@gmail.com>
  1 sibling, 1 reply; 9+ messages in thread

From: Adrian Klaver @ 2018-05-08 04:10 UTC (permalink / raw)
  To: tango ward <tangoward15@gmail.com>; pgsql-general@lists.postgresql.org

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




^ permalink  raw  reply  [nested|flat] 9+ messages in thread

* Re: KeyError: self._index[x]
@ 2018-05-08 04:40  tango ward <tangoward15@gmail.com>
  parent: Adrian Klaver <adrian.klaver@aklaver.com>
  0 siblings, 1 reply; 9+ messages in thread

From: tango ward @ 2018-05-08 04:40 UTC (permalink / raw)
  To: Adrian Klaver <adrian.klaver@aklaver.com>; David G. Johnston <david.g.johnston@gmail.com>; pgsql-general@lists.postgresql.org

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
>

^ permalink  raw  reply  [nested|flat] 9+ messages in thread

* Re: KeyError: self._index[x]
@ 2018-05-08 04:42  tango ward <tangoward15@gmail.com>
  parent: tango ward <tangoward15@gmail.com>
  0 siblings, 1 reply; 9+ messages in thread

From: tango ward @ 2018-05-08 04:42 UTC (permalink / raw)
  To: Adrian Klaver <adrian.klaver@aklaver.com>; David G. Johnston <david.g.johnston@gmail.com>; pgsql-general@lists.postgresql.org

Yeah you're right, making 'translate' as the key works. Thanks for pointing
this out Sir David "That said, by default the name of columns whose values
are derived by a single function call should be the name of the function."
I didn't know it. Is it only in psycopg2 that the name of the columns will
use the name of the function?

On Tue, May 8, 2018 at 12:40 PM, tango ward <tangoward15@gmail.com> wrote:

> 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
>>
>
>

^ permalink  raw  reply  [nested|flat] 9+ messages in thread

* Re: KeyError: self._index[x]
@ 2018-05-08 04:58  David G. Johnston <david.g.johnston@gmail.com>
  parent: tango ward <tangoward15@gmail.com>
  0 siblings, 1 reply; 9+ messages in thread

From: David G. Johnston @ 2018-05-08 04:58 UTC (permalink / raw)
  To: tango ward <tangoward15@gmail.com>; +Cc: Adrian Klaver <adrian.klaver@aklaver.com>; pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>

On Monday, May 7, 2018, tango ward <tangoward15@gmail.com> wrote:

>  I didn't know it. Is it only in psycopg2 that the name of the columns
> will use the name of the function?
>

The server assigns column names - hence the advice to use psql to
investigate SQL issues more easily since there is one less moving part to
deal with.

David J.

^ permalink  raw  reply  [nested|flat] 9+ messages in thread

* Re: KeyError: self._index[x]
@ 2018-05-08 05:00  tango ward <tangoward15@gmail.com>
  parent: David G. Johnston <david.g.johnston@gmail.com>
  0 siblings, 0 replies; 9+ messages in thread

From: tango ward @ 2018-05-08 05:00 UTC (permalink / raw)
  To: David G. Johnston <david.g.johnston@gmail.com>; +Cc: Adrian Klaver <adrian.klaver@aklaver.com>; pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>

Thanks, now I understand. Thank you so much for being so helpful to a
newbie same with Sir Adrian.

On Tue, May 8, 2018 at 12:58 PM, David G. Johnston <
david.g.johnston@gmail.com> wrote:

> On Monday, May 7, 2018, tango ward <tangoward15@gmail.com> wrote:
>
>>  I didn't know it. Is it only in psycopg2 that the name of the columns
>> will use the name of the function?
>>
>
> The server assigns column names - hence the advice to use psql to
> investigate SQL issues more easily since there is one less moving part to
> deal with.
>
> David J.
>
>

^ permalink  raw  reply  [nested|flat] 9+ messages in thread


end of thread, other threads:[~2018-05-08 05:00 UTC | newest]

Thread overview: 9+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2018-05-08 03:07 KeyError: self._index[x] tango ward <tangoward15@gmail.com>
2018-05-08 03:11 ` tango ward <tangoward15@gmail.com>
2018-05-08 03:50   ` David G. Johnston <david.g.johnston@gmail.com>
2018-05-08 04:06     ` Adrian Klaver <adrian.klaver@aklaver.com>
2018-05-08 04:10   ` Adrian Klaver <adrian.klaver@aklaver.com>
2018-05-08 04:40     ` tango ward <tangoward15@gmail.com>
2018-05-08 04:42       ` tango ward <tangoward15@gmail.com>
2018-05-08 04:58         ` David G. Johnston <david.g.johnston@gmail.com>
2018-05-08 05:00           ` tango ward <tangoward15@gmail.com>

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