473,387 Members | 1,535 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

List comprehension and FieldStorage

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

Jul 19 '05 #1
4 2106
bump

Jul 19 '05 #2
Derek Basch wrote:
bump


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
Jul 19 '05 #3
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.

Jul 19 '05 #4
Derek Basch wrote:
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

Jul 19 '05 #5

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

Similar topics

23
by: Fuzzyman | last post by:
Pythons internal 'pointers' system is certainly causing me a few headaches..... When I want to copy the contents of a variable I find it impossible to know whether I've copied the contents *or*...
35
by: Moosebumps | last post by:
Does anyone here find the list comprehension syntax awkward? I like it because it is an expression rather than a series of statements, but it is a little harder to maintain it seems. e.g. you...
7
by: Chris P. | last post by:
Hi. I've made a program that logs onto a telnet server, enters a command, and then creates a list of useful information out of the information that is dumped to the screen as a result of the...
6
by: jena | last post by:
hello, when i create list of lambdas: l=] then l() returns 'C', i think, it should be 'A' my workaround is to define helper class with __call__ method: class X: def __init__(self,s): self.s=s...
18
by: a | last post by:
can someone tell me how to use them thanks
4
by: Gregory Guthrie | last post by:
Sorry for a simple question- but I don't understand how to parse this use of a list comprehension. The "or" clauses are odd to me. It also seems like it is being overly clever (?) in using a...
4
by: bullockbefriending bard | last post by:
Given: class Z(object): various defs, etc. class ZList(list): various defs, etc. i would like to be able to replace
10
by: Debajit Adhikary | last post by:
I have two lists: a = b = What I'd like to do is append all of the elements of b at the end of a, so that a looks like: a =
4
by: beginner | last post by:
Hi All, If I have a list comprehension: ab= c = "ABC" print c
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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
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,...

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.