value exists (newbie question)

John Machin sjmachin at lexicon.net
Wed Sep 20 02:31:56 CEST 2006


eldorado wrote:
> Hello,
>
> I am trying to parse some files so that if a postal code exists, but is
> longer than five digits it will return me only the first five digits:
> ...
> for insDict in insureDict:
>  	insDict['postalcode'] = insDict.get('postalcode')[:5]
> ...
> This works, except for when I get a blank postalcode.  In which case I get
(> the following error.
> ERR exceptions.TypeError: iteration over non-sequence

What is "blank"? Do you mean "empty" as in zero-length, or "contains
one or more spaces" as in "      " or do you mean "is None"? Which
statement causes the error? Could you possibly provide the traceback?

If an insDict has no key 'postalcode', then the .get() will return
None, and that insDict will have the equivalent of:
     insDict['postalcode'] = None
done to it, which may not be what you want.

However this will not of itself cause "TypeError: iteration over
non-sequence" ... either

(1) insureDict is not iterable -- BTW insureDict is a strangely chosen
name; it implies it is a dictionary, BUT insDict is quite obviously a
dict, which can't be used as a key for another dict .... what is
type(insureDict)?

or (2) you are getting this error later by trying to iterate over the
values in insDict dicts -- you think the values are all strings but one
or more contain None ... show us the traceback!!

>
> What I would like to do is to check to make sure a value exists.

Such a *what* value exists *where*?

HTH,
John




More information about the Python-list mailing list