can you iterate over a FieldStorage object?

John Salerno johnjsal at
Fri Jun 2 21:15:15 CEST 2006

I'm trying to use a for loop with a FieldStorage object and I get the 
following error. Can you not treat it like a dictionary, or am I writing 
the for loop incorrectly?

for item in form:
     print item
     # or print item.value

KeyError	Python 2.2.1: /usr/bin/python
Fri Jun 2 15:12:36 2006

A problem occurred in a Python script. Here is the sequence of function 
calls leading up to the error, in the order they occurred.

     6 print 'content-type: text/html\n'
     8 form = cgi.FieldStorage(keep_blank_values=True)
     9 for item in form:
    10         print item
item undefined, form = FieldStorage(None, None, 
[MiniFieldStorage('firs...02116'), MiniFieldStorage('foreignAddress', '')])

  /usr/lib/python2.2/ in __getitem__(self=FieldStorage(None, None, 
[MiniFieldStorage('firs...02116'), MiniFieldStorage('foreignAddress', 
'')]), key=0)
   548             if == key: found.append(item)
   549         if not found:
   550             raise KeyError, key
   551         if len(found) == 1:
   552             return found[0]
KeyError undefined, key = 0

KeyError: 0
       __doc__ = 'Mapping key not found.'
       __getitem__ = <bound method KeyError.__getitem__ of 
<exceptions.KeyError instance at 0x81633e4>>
       __init__ = <bound method KeyError.__init__ of 
<exceptions.KeyError instance at 0x81633e4>>
       __module__ = 'exceptions'
       __str__ = <bound method KeyError.__str__ of <exceptions.KeyError 
instance at 0x81633e4>>
       args = (0,)

More information about the Python-list mailing list