List comprehension and FieldStorage

D

Derek Basch

Given this FieldStorage object:

FieldStorage(None, None, [MiniFieldStorage('Checkbox1', 'on'),
MiniFieldStorage('Checkbox2', 'on')])

I am trying to cgi.urlencode the FieldStorage object to POST to another
cgi script. The documentation for urlencode,
http://docs.python.org/lib/module-urllib.html , says that urlencode
takes a mapping object or a list of double tuples. I see that I could
use cgi.parse_qs and cgi.parse_qsl to create these object types. Should
I just use that or should I continue using FieldStorage? Also how would
I create an object of either type from a FieldStorage object? Here is
my attempt at creating the double tuple list (It sucks....I know):

print [(key, value) for key, value in form.keys() and form[key].value]

Thanks for the help,
Derek Basch
 
P

Peter Hansen

Derek said:

If "bump" is supposed to be some kind of nudge to get people to reply,
please have more patience. You posted only sometime late yesterday, and
many people take up to a few days to receive the posts from this
newsgroup, and even those who don't shouldn't be expected to reply the
same instant they read your question. Also, it's quite possible you
won't get any reply (though this is pretty rare) in which case "bump" is
going to be seen as nothing more than rudeness, given that if an answer
was forthcoming it would probably already have been posted. In such a
case, you're better off rereading your request and considering whether
you phrased it adequately or can supply additional information to help
potential responders.

(In this case, your question looks fairly clear, though it doesn't look
like you've spent much time at the interactive prompt experimenting and
trying to find an answer on your own.)

-Peter
 
D

Derek Basch

Sorry Peter. I will refrain from nudging in the future. I did spend
time at the interactive prompt and got nothing. Maybe I will have
better luck next time.
 
P

Peter Otten

Derek said:
print [(key, value) for key, value in form.keys() and form[key].value]
and

bump

The tutorial is always willing to make time for you :)
import cgi
fs = cgi.FieldStorage(environ=dict(QUERY_STRING="alpha=1&beta=2"))
[(key, fs[key].value) for key in fs.keys()]
[('alpha', '1'), ('beta', '2')]

So you might have come up yourself with the above in the mean time.
However, it breaks if there are multiple occurrences of the same key:
fs = cgi.FieldStorage(environ=dict(QUERY_STRING="alpha=1&beta=2&alpha=3"))
[(key, fs[key].value) for key in fs.keys()]
Traceback (most recent call last):
File "<stdin>", line 1, in ?
AttributeError: 'list' object has no attribute 'value'

A quick glance in the source suggests that
[(mfs.name, mfs.value) for mfs in fs.list]
[('alpha', '1'), ('beta', '2'), ('alpha', '3')]

is a bit more robust. But perhaps you can get hold of the original unparsed
query string and avoid the detour via FieldStorage altogether?

Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,967
Messages
2,570,148
Members
46,694
Latest member
LetaCadwal

Latest Threads

Top