Re: KeyError: self._index[x]

From: Adrian Klaver <adrian(dot)klaver(at)aklaver(dot)com>
To: "David G(dot) Johnston" <david(dot)g(dot)johnston(at)gmail(dot)com>, tango ward <tangoward15(at)gmail(dot)com>
Cc: "pgsql-general(at)lists(dot)postgresql(dot)org" <pgsql-general(at)lists(dot)postgresql(dot)org>
Subject: Re: KeyError: self._index[x]
Date: 2018-05-08 04:06:55
Message-ID: 661c8f00-ba13-3806-b7bd-2f05159df9b4@aklaver.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On 05/07/2018 08:50 PM, David G. Johnston wrote:
> On Monday, May 7, 2018, tango ward <tangoward15(at)gmail(dot)com
> <mailto:tangoward15(at)gmail(dot)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(dot)klaver(at)aklaver(dot)com

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Adrian Klaver 2018-05-08 04:10:31 Re: KeyError: self._index[x]
Previous Message David G. Johnston 2018-05-08 03:50:14 Re: KeyError: self._index[x]