Troubleshooting: re.finditer() creates object even when no match found

Steven Bethard steven.bethard at gmail.com
Sat Dec 18 05:54:36 EST 2004


Nick Coghlan wrote:
> Nick Coghlan wrote:
> 
>> Chris Lasher wrote:
>>
>>> Hello,
>>> I really like the finditer() method of the re module. I'm having
>>> difficulty at the moment, however, because finditer() still creates a
>>> callable-iterator oject, even when no match is found. This is
>>> undesirable in cases where I would like to circumvent execution of code
>>> meant to parse out data from my finditer() object.
>>
>> Take a look at itertools.tee
> 
> Bleh - I hit send instead of delete. Tee probably doesn't do what you 
> want. Steve's cookbook recipe is likely a better option.

Actually, there's an equally valid solution with tee too -- check Peter 
Otten's comments at the bottom of the recipe.

Steve



More information about the Python-list mailing list