Reputation: 33
I want to write code that locates every line in the file that begins with “From”, and extracts the portion of the e-mail address located between white space and the “@” symbol.
text = open('mBox.txt')
for line in text:
if line.startswith('From') :
print line
This is what I have so far. Not sure how to make it extract the specific part I want. This is for python 2.7
Upvotes: 0
Views: 153
Reputation: 50220
Use a regular expression:
for line in text:
if line.startswith("From"):
match = re.search(r"(\S+)@", line)
if match:
print match.group(1)
The \S
matches any character that is not a space. Since \S+
will match as much as possible, there's no need to mention the preceding space.
Upvotes: 1
Reputation: 230
you can do it this way
text = open('mBox.txt')
for line in text:
if line.split(' ', 1)[0] == 'From':
.....
Upvotes: 0
Reputation: 114068
print line.split("From",1)[-1].split("@").strip()
should do what you want .... its one way at least
you could also do it with regex
matches = []
matcher=lambda m:matches.append(m.groups(1)) or ""
re.sub("^From\s+(\w+)@.*",text.read())
print matches
Upvotes: 0