[Tutor] KeyError?

James Reynolds eire1130 at gmail.com
Thu Jul 28 18:42:44 CEST 2011


On Thu, Jul 28, 2011 at 12:11 PM, Prasad, Ramit
<ramit.prasad at jpmchase.com>wrote:

> *From:* tutor-bounces+ramit.prasad=jpmchase.com at python.org [mailto:
> tutor-bounces+ramit.prasad=jpmchase.com at python.org] *On Behalf Of *Shwinn
> Ricci
> *Sent:* Thursday, July 28, 2011 10:51 AM
> *To:* tutor at python.org
> *Subject:* [Tutor] KeyError?****
>
> ** **
>
> I have an excel file that I am reading cell values from and putting them
> into a dictionary. the dictionary looks like this:
>
> scafPositions = {position[j]: direction[j]}
>
> where position[j] is exclusively floating/numerical values and direction[j]
> is exclusively strings.
>
> When I try to find whether a test value val is in the array of positions,
> and then try to look its direction up with ScafPositions[val], I get a
> KeyError. Is this because my data isn't standalone numbers, but numbers that
> are calculated based off other cells? Or can I not test for val?****
>
> ======================================****
>
> ** **
>
> Your problem is probably that you are reassigning a new dictionary to the
> name scafPositions each time instead of updating it. You should have
> something like the following.****
>
> ** **
>
> scafPositions = {}****
>
> ** **
>
> #loop structure here:****
>
>     #do stuff****
>
>     scafPositions[ position[j] ] = direction[j]****
>
>
>



Or it could be it's not introspecting the formula within excel and his key
is the string "=A1*sheet2!$B$1"or it could be he's looking for a float / int
when in reality it's a string (because it must be converted explicitly).

Without a traceback and the code, it's hard to know for sure.

My guess is leading towards introspection.

As far as printing the contents, once you create your dict with all the data
just use the print statement / function depending on your version of python

print scafPositions or print(scafPositions)

You could loop through it as well

for key, value in scafPositions.items():
    print key, value

This will give you an idea of what is in the dict.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110728/929bd87e/attachment.html>


More information about the Tutor mailing list