<br><br><div class="gmail_quote">On Wed, Apr 15, 2009 at 9:46 AM, Gilles Ganault <span dir="ltr"><<a href="mailto:nospam@nospam.com">nospam@nospam.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Hello<br>
<br>
I stumbled upon something funny while downloading web pages and<br>
trying to extract one or more blocks from a page: Even though Python<br>
seems to return at least one block, it doesn't actually enter the for<br>
loop:<br>
<br>
======<br>
re_block = re.compile('before (.+?) after',re.I|re.S|re.M)<br>
<br>
#Here, get web page and put it into "response"<br>
<br>
blocks = None<br>
blocks = re_block.finditer(response)<br>
if blocks == None:<br>
print "No block found"<br>
else:<br>
print "Before blocks"<br>
for block in blocks:<br>
#Never displayed!<br>
print "In blocks"<br>
======<br>
<br>
Since "blocks" is no longer set to None after calling finditer()...<br>
but doesn't contain a single block... what does it contain then?<br>
<br>
Thank you for any tip.<br>
<font color="#888888">--<br>
<a href="http://mail.python.org/mailman/listinfo/python-list" target="_blank">http://mail.python.org/mailman/listinfo/python-list</a><br>
</font></blockquote></div><br><br>It contains an iterator. It's just an iterator with nothing in it:<br><br>In [232]: import re<br><br>In [233]: re_block = re.compile('before (.+?) after',re.I|re.S|re.M)<br><br>
In [234]: blocks = None<br><br>In [235]: blocks = re_block.finditer('Hi There Im not going to match anything')<br><br>In [236]: blocks<br>Out[236]: <callable-iterator object at 0xb75f440c><br><br>In [237]: blocks == None<br>
Out[237]: False<br><br>In [238]: for block in blocks:<br> .....: print block<br> .....: <br><br>In [239]: type(blocks)<br>Out[239]: <type 'callable-iterator'><br><br>In [241]: blocks.next()<br>---------------------------------------------------------------------------<br>
StopIteration Traceback (most recent call last)<br><br>/home/jeremiah/<ipython console> in <module>()<br><br>StopIteration: <br><br><br>Maybe you should just use a different method, like findall, where you can check the length of it. Or, if you don't need to do anything special when there aren't any blocks, you could continue like this.<br>