473,513 Members | 2,490 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

beginner python cgi question

Hi folks,

I am trying to learn python, and have a form processing script giving me
this error:

Traceback (most recent call last):
File "inquiry", line 16, in ?
Address=fields["address"].value
File "/usr/local/python2.2/lib/python2.2/cgi.py", line 550, in __getitem__
raise KeyError, key
KeyError: address

This is the snippet of code the error is referring to:

....snip...
fields=cgi.FieldStorage()
if (fields.has_key("name") and fields.has_key("emailaddr")):
SenderName=fields["name"].value
Address=fields["address"].value
City=fields["city"].value
State=fields["state"].value
....snip...

I think what the problem is that if my form does not have one of the
fields (except 'name' and 'emailaddr') with data in it, I get this error.
What is the best way to fix this?

Please be kind, this is my first attempt at python. Thanks for the help!

Brandon
Jul 18 '05 #1
5 1821
Brandon Boles:
Address=fields["address"].value [...]KeyError: address [...] if (fields.has_key("name") and fields.has_key("emailaddr")):
SenderName=fields["name"].value
Address=fields["address"].value
City=fields["city"].value
State=fields["state"].value
...snip...

I think what the problem is that if my form does not have one of the
fields (except 'name' and 'emailaddr') with data in it, I get this error.
What is the best way to fix this?


One way is to use if has_key(), as you already discovered. The other way
is exception handling:

try:
Address = fields["address"].value
except KeyError, e:
Address = None # and/or report an error
else:
# do something with Address

--
René Pijlman
Jul 18 '05 #2
+--[ Brandon Boles ]---[ em***@nowhere.foo ]
| ...snip...
| fields=cgi.FieldStorage()
| if (fields.has_key("name") and fields.has_key("emailaddr")):
| SenderName=fields["name"].value
| Address=fields["address"].value
| City=fields["city"].value
| State=fields["state"].value
| ...snip...
|
| I think what the problem is that if my form does not have one of the
| fields (except 'name' and 'emailaddr') with data in it, I get this error.
| What is the best way to fix this?

Getting a default value, if "address" is not set:

Address = fields.get("address", "n/a")

jochen
Jul 18 '05 #3
+--[ Brandon Boles ]---[ em***@nowhere.foo ]
| ...snip...
| fields=cgi.FieldStorage()
| if (fields.has_key("name") and fields.has_key("emailaddr")):
| SenderName=fields["name"].value
| Address=fields["address"].value
| City=fields["city"].value
| State=fields["state"].value
| ...snip...
|
| I think what the problem is that if my form does not have one of the
| fields (except 'name' and 'emailaddr') with data in it, I get this error.
| What is the best way to fix this?

Getting a default value, if "address" is not set:

Address = fields.get("address", "n/a")

jochen
Jul 18 '05 #4
Use 'fields=cgi.FieldStorage(keep-blank-values=1)' and you don't have to
worry about KeyErrors anymore.

Hope this helps.

Paolo

Brandon Boles wrote:
Hi folks,

I am trying to learn python, and have a form processing script giving me
this error:

Traceback (most recent call last):
File "inquiry", line 16, in ?
Address=fields["address"].value
File "/usr/local/python2.2/lib/python2.2/cgi.py", line 550, in __getitem__
raise KeyError, key
KeyError: address

This is the snippet of code the error is referring to:

...snip...
fields=cgi.FieldStorage()
if (fields.has_key("name") and fields.has_key("emailaddr")):
SenderName=fields["name"].value
Address=fields["address"].value
City=fields["city"].value
State=fields["state"].value
...snip...


Jul 18 '05 #5
Jochen Wersdörfer <jo****@helferlein.net> wrote:
Address = fields.get("address", "n/a")


(You mean fields.getvalue, natch.)

If you don't want to have to deal with cgi returning a list sometimes, what
you really want is fields.getfirst('fieldname', 'default'), but this only
exists in Python 2.2 onwards.

--
Andrew Clover
mailto:an*@doxdesk.com
http://www.doxdesk.com/
Jul 18 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2852
by: Art | last post by:
NEWBIE ALERT! Esteemed List Participants and Lurkers: (System: P-II 350, 192 meg, Win98 SE, Python 2.2.3, wxPythonWIN32-2.4.1.2-Py22.exe) I'm having a lot of fun getting started with Python...
1
1571
by: sarmin | last post by:
Hi Gurus... I am using pythonwin to write the programming code and boa constructor to build the GUI... the GUI and the python code work fine when run through pythonwin platform... i m now...
46
3460
by: J.R. | last post by:
Hi folks, The python can only support passing value in function call (right?), I'm wondering how to effectively pass a large parameter, such as a large list or dictionary? It could achieved...
11
2919
by: vdavidster | last post by:
Hello everyone, I want to convert a tuple to a list, and I expected this behavior: list(('abc','def')) -> list(('abc')) -> But Python gave me this behavior: list(('abc','def')) ->
44
4126
by: lester | last post by:
a pre-beginner's question: what is the pros and cons of .net, compared to ++ I am wondering what can I get if I continue to learn C# after I have learned C --> C++ --> C# ?? I think there...
2
1493
by: John Salerno | last post by:
Ok, like I mentioned before, I'm learning C# for fun. I'm interested in learning Python sort of as a "supplement" (by that, I mean a language with scripting capabilities that can do things maybe...
6
2981
by: Qun Cao | last post by:
Hi Everyone, I am a beginner on cross language development. My problem at hand is to build a python interface for a C++ application built on top of a 3D game engine. The purpose of this python...
11
1186
by: bambam | last post by:
Would someone like to suggest a replacement for this? This is a function that returns different kinds of similar objects, depending on what is asked for. PSP and PWR are classes. I don't really...
9
1509
by: SMALLp | last post by:
Hy! I have error something like this TypeError: unbound method insert() must be called with insertData instance as first argument (got str instance instead) CODE: File1.py...
3
2028
by: Ben Keshet | last post by:
I have a probably simple beginner's question - I have a script that I am currently able to print its output. instead, i want to write it into a file - I tried different versions of write() but...
0
7260
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7160
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7384
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7099
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7525
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5685
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5086
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3222
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1594
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.