Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!newsgate.cistron.nl!newsgate.news.xs4all.nl!194.109.133.85.MISMATCH!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.006 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'happily': 0.07; 'something,': 0.07; 'builtin': 0.09; 'dict': 0.09; 'function:': 0.09; 'rename': 0.09; 'tuple': 0.09; 'am,': 0.12; 'element,': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'imho,': 0.16; 'subject:values': 0.16; 'wrote:': 0.16; 'wed,': 0.17; 'level,': 0.18; 'header:In-Reply-To:1': 0.22; 'dictionary': 0.23; 'aug': 0.24; 'skip:" 30': 0.28; 'import': 0.28; 'message- id:@mail.gmail.com': 0.29; '(and': 0.29; 'module': 0.30; 'line:': 0.30; 'url:library': 0.31; 'seem': 0.31; 'chris': 0.32; 'list': 0.32; 'probably': 0.33; 'to:addr:python-list': 0.33; '17,': 0.34; 'reference': 0.35; 'url:python': 0.36; '(by': 0.37; 'but': 0.37; 'could': 0.38; 'received:google.com': 0.38; 'url:org': 0.38; 'received:209.85': 0.38; 'should': 0.38; 'skip:o 20': 0.38; 'subject:: ': 0.39; 'url:docs': 0.39; "there's": 0.39; 'to:addr:python.org': 0.39; 'or,': 0.40; 'where': 0.40; 'your': 0.61; 'here.': 0.66; '10:19': 0.84; 'difference.': 0.84; 'url:functions': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=266OUnk+7fMCB0/s80EKFd6xmUSAhE53GNqnVDKBizE=; b=mYM3ea2YfdkGWJpqm1hyIITukmc2vmJv1aKNqkx8RFL/j28H2QUa9Li9lzntrCoF9h 6QBse8lgPRfx7QPpei3IjXIAw6Nx75+sNLsTgtm5ske5KycoVtONsuNE2tnuBR1OkEXC Mg8H/Vd92Cot3Hx7sGYf+/AroTroT0L5rrP9w= MIME-Version: 1.0 In-Reply-To: References: <92d066ff-d0ee-432f-b512-1f2fa01ba681@a12g2000yqi.googlegroups.com> Date: Wed, 17 Aug 2011 10:41:41 +0100 Subject: Re: CGI: Assign FieldStorage values to variables From: Chris Angelico To: python-list@python.org Content-Type: text/plain; charset=ISO-8859-1 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 34 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1313574104 news.xs4all.nl 23965 [2001:888:2000:d::a6]:47049 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:11672 On Wed, Aug 17, 2011 at 10:19 AM, Gnarlodious wrote: > import cgi, os > os.environ["QUERY_STRING"] = "name1=Val1&name2=Val2&name3=Val3" > form=cgi.FieldStorage() > > form > > dict = {} > for key in form.keys(): dict[ key ] = form[ key ].value > You could probably use a list comp for this, but there's not a lot of difference. (By the way, you should be aware that you're shadowing the builtin 'dict' here. That's not a problem, but be aware.) dict.update(((key,form[key].value) for key in form)) That's a generator that returns a (key,value) tuple for each form element, which update() will happily use. But the last line: > locals().update(dict) is, IMHO, a very bad idea. Rename your dictionary to 'request' or 'data' or 'form' or something, and then reference form['name3'] instead; or, be explicit: name3=form['name3'] Incidentally, you seem to be working at the module level, where locals() is globals() (and I mean that literally - 'locals() is globals()' is True). You may not be able to do this with locals() in a function: http://docs.python.org/library/functions.html#locals Chris Angelico