Re: KeyError: self._index[x]

From: tango ward <tangoward15(at)gmail(dot)com>
To: Adrian Klaver <adrian(dot)klaver(at)aklaver(dot)com>, "David G(dot) Johnston" <david(dot)g(dot)johnston(at)gmail(dot)com>, pgsql-general(at)lists(dot)postgresql(dot)org
Subject: Re: KeyError: self._index[x]
Date: 2018-05-08 04:42:52
Message-ID: CAA6wQLK50tShVbkLXk9crpU=CQ4+Dh9dV-B8TPSdev5qg5Shfg@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

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(at)gmail(dot)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(dot)klaver(at)aklaver(dot)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(dot)klaver(at)aklaver(dot)com
>>
>
>

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message David G. Johnston 2018-05-08 04:58:33 Re: KeyError: self._index[x]
Previous Message tango ward 2018-05-08 04:40:11 Re: KeyError: self._index[x]