
April 25th, 2008, 09:18 AM
|
|
Contributing User
|
|
Join Date: Apr 2006
Posts: 30
Time spent in forums: 10 h 51 m 50 sec
Reputation Power: 3
|
|
|
Regex Loop
Data file contains:
Code:
age : 35
name : Bob
age : 22
name : Bill
Python Code:
Original
- Python Code |
|
|
|
#!/usr/bin/python import re data = open('./data') data = data.read() output = re.search(r"(?<=name).*", data).group().split(':') output = output[1].strip() print output
This outputs:
But, if I try and loop through it to match both names:
Python Code:
Original
- Python Code |
|
|
|
#!/usr/bin/python import re data = open('./data') data = data.read().split('\n') for x in data: name = re.search(r"(?<=name).*", x).group().split(':') print name
This returns:
Code:
AttributeError: 'NoneType' object has no attribute 'group'
I am pretty sure it's making a match, because I can get:
Code:
None
<_sre.SRE_Match object at 0xb7eaaf38>
None
None
<_sre.SRE_Match object at 0xb7eaaf38>
None
None
Is this not the proper way to try and loop through the matches?
Thanks 
|