Thread: KeyError: self._index[x]
Good day,
Apologies for asking again. `
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 """)
`
Sorry, accidentally pressed send.
print row['snumber']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 """)
cur_t = conn_t.cursor(cursor_factory=
cur_t.execute("""
SELECT TRANSLATE(snumber, ' ', '')
FROM sprofile """)
# This will result in KeyError
for row in cur_t:# This works fine
for row in cur_t:
print row[0]
Sorry again.
I would really appreciate any suggestions.
Thanks,
J
On Monday, May 7, 2018, tango ward <tangoward15@gmail.com> wrote:
print row['snumber']
cur_t.execute("""
SELECT TRANSLATE(snumber, ' ', '')
FROM sprofile """)# This will result in KeyErrorfor row in cur_t:# This works finefor 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.
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
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
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
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
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.
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.