Multiline regex help

Kent Johnson kent37 at tds.net
Thu Mar 3 21:49:39 EST 2005


Steven Bethard wrote:
> Kent Johnson wrote:
> 
>> for line in raw_data:
>>     if line.startswith('RelevantInfo1'):
>>         info1 = raw_data.next().strip()
>>     elif line.startswith('RelevantInfo2'):
>>         info2 = raw_data.next().strip()
>>     elif line.startswith('RelevantInfo3'):
>>         info3 = raw_data.next().strip()
>>         scores.setdefault(info1, {}).setdefault(info3, []).append(info2)
>>         info1 = info2 = info3 = None
> 
> 
> Very pretty. =)  I have to say, I hadn't ever used iterators this way 
> before, that is, calling their next method from within a for-loop.  I 
> like it. =)

I confess I have a nagging suspicion that someone who actually knows something about CPython 
internals will tell me why it's a bad idea...but it sure is handy!

> Thanks for opening my mind. ;)

My pleasure :-)

Kent



More information about the Python-list mailing list