From: Tim Chase on
> I need to do something like the following:
>
> pat = re.compile('edit[0-9]*:[0-9]*')
> check = form.getfirst(pat)
>
> (to check things like 'edit0:1') How do I do this?

Well, you can do it either as

check = pat.search(string_to_search)

which is pretty plainly detailed in the help for the "re" module,
both under the search() function and under the "8.2.2 Matching
vs. Searching" section.

Alternatively, if you plan to get the others too, you can use

fi = pat.finditer(string_to_search)
check = fi.next().group(0) # beware this may throw
# a StopIteration if there are no more matches.

which would usually be done inside a loop where the StopIteration
does the Right Thing(tm).

But if you're using it on HTML form text, regexps are usually the
wrong tool, and you should be using an HTML parser (such as
BeautifulSoup) that knows how to handle odd text and escapings
better and more robustly than regexps will.

-tkc



From: Tim Chase on
Victor Subervi wrote:
> On Wed, Jan 6, 2010 at 1:27 PM, Tim Chase <python.list(a)tim.thechases.com>wrote:
>
>> But if you're using it on HTML form text, regexps are usually the wrong
>> tool, and you should be using an HTML parser (such as BeautifulSoup) that
>> knows how to handle odd text and escapings better and more robustly than
>> regexps will
>
> I have an automatically generated HTML form from which I need to extract
> data to the script which this form calls (to which the information is sent).
> I believe BeautifulSoup is geared to scraping pages that exist permanently
> on the web. By the time BeautifulSoup was called, this page would be gone.

BeautifulSoup takes string data fed to it, and builds a structure
that can be neatly navigated. That string data can come from a
web page, from a disk, or even a serial port, a
random-character-generator, or just from HTML that's built up in
memory and never sees a network or a disk. It's worth reading
its documentation[1] and trying its examples to get familiar with it.

-tkc


[1]
http://www.crummy.com/software/BeautifulSoup/documentation.html



From: Carsten Haese on
Victor Subervi wrote:
> I have an automatically generated HTML form from which I need to extract
> data to the script which this form calls (to which the information is
> sent).

Ideally, the script that receives the submitted fields should know how
the form was generated, so it knows what fields to expect. Failing that,
you should realize that getfirst() is not the only way to access the
contents of a cgi.FieldStorage object.

If you need to know the values of all fields whose names obey a certain
pattern, I recommend you start with a list of all field names in the
FieldStorage object and pick out the ones whose names obey that pattern.
To get the list of field names, you should consider using the .keys()
method of the FieldStorage object.

-Carsten

From: Carsten Haese on
Victor Subervi wrote:
> Code snippet:
> [...]
>
> Error:
> [...]
> What do?

After eliminating the pieces of your post that have been copied or
quoted from elsewhere, I am left with a total of five words of your own
to ask this question, which seems to be approximately the same amount of
effort you have put into trying to figure out what's causing the error.

If you put in a little bit more effort than that, you might figure out
yourself what's causing the error, which should then logically lead you
to how to fix the error.

Good luck.

-Carsten

From: Steve Holden on
Victor Subervi wrote:
> On Fri, Jan 8, 2010 at 10:11 AM, Carsten Haese <carsten.haese(a)gmail.com
> <mailto:carsten.haese(a)gmail.com>> wrote:
>
> Victor Subervi wrote:
> > Code snippet:
> > [...]
> >
> > Error:
> > [...]
> > What do?
>
> After eliminating the pieces of your post that have been copied or
> quoted from elsewhere, I am left with a total of five words of your own
> to ask this question, which seems to be approximately the same amount of
> effort you have put into trying to figure out what's causing the error.
>
>
> First I get scolded for not including enough information. Now I get
> scolded for including too much. Seems I can't win. I *am* putting effort
> into understanding this. I'm sorry I'm not as sharp as you are in these
> matters.
>
> params = {}, key = 'store', global cgi = <module 'cgi' from
> '/usr/lib64/python2.4/cgi.pyc'
>>, cgi.FieldStorage = <class cgi.FieldStorage>, ].value undefined
>
> TypeError: unsubscriptable object
> args = ('unsubscriptable object',)
>
> Why is the value undefined? What is an unsubscriptable object?
> TIA,
> beno
>
What is "why"?

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
PyCon is coming! Atlanta, Feb 2010 http://us.pycon.org/
Holden Web LLC http://www.holdenweb.com/
UPCOMING EVENTS: http://holdenweb.eventbrite.com/

 |  Next  |  Last
Pages: 1 2 3
Prev: TypeError
Next: QDoubleValidator