KeyError: self._index[x]
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:
`
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
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.
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:
Show quoted text
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:
Show quoted text
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:
Show quoted text
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.