[Tutor] KeyError?

Walter Prins wprins at gmail.com
Thu Jul 28 18:00:11 CEST 2011


On 28 July 2011 16:50, Shwinn Ricci <armvrt at gmail.com> wrote:

> 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?
>

http://wiki.python.org/moin/KeyError

I quote:
"Python raises a *KeyError* whenever a dict() object is requested (using the
format a = adict[key]) and the key is not in the dictionary. If you don't
want to have an exception but would rather a default value used instead, you
can use the get() method: "

Walter
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110728/de96e5e0/attachment.html>


More information about the Tutor mailing list