Something weird about re.finditer()

John Machin sjmachin at
Wed Apr 15 11:09:10 CEST 2009

On Apr 15, 6:46 pm, Gilles Ganault <nos... at> wrote:
> Hello
>         I stumbled upon something funny while downloading web pages and
> trying to extract one or more blocks from a page: Even though Python
> seems to return at least one block, it doesn't actually enter the for
> loop:
> ======
> re_block = re.compile('before (.+?) after',re.I|re.S|re.M)
> #Here, get web page and put it into "response"
> blocks = None
> blocks = re_block.finditer(response)
> if blocks == None:
>         print "No block found"
> else:
>         print "Before blocks"
>         for block in blocks:
>                 #Never displayed!
>                 print "In blocks"
> ======
> Since "blocks" is no longer set to None after calling finditer()...
> but doesn't contain a single block... what does it contain then?
> Thank you for any tip.

Tip 0: contemplate what type you could infer from the name findITER
Tip 1: Read the manual to see what type is returned by re.finditer
(or do import re; help(re.finditer))
Tip 2: Append
   , type(blocks)
to the relevant print statements in your above code, and inspect the

Metatip 0: Following the tips can be done rapidly without any need for
an internet connection.

Meta**2tip 0: The Tips and the Metatip can be applied to many things,
not just re.finditer.


More information about the Python-list mailing list