newbie parsing question

K

Kun

i have a list of that is:

[('460 (BODY[HEADER.FIELDS (FROM)] {46}', 'From: Friend
<[email protected]>\r\n\r\n'), ')', ('462 (BODY[HEADER.FIELDS (FROM)] {37}',
'From: Kun <[email protected]>\r\n\r\n'), ')']


how do i parse the email addresses out of it into another list or string?
 
K

Kun

i tried to parse (below) with the regular expression: emails =
re.findall('\S*\s(\w+@\w+\.\w+)', senders)

and got the following error:

Traceback (most recent call last):
File "/Life/School/Homework/Spring 2006/OPIM
399/Tutorial/IMAP/scannermailer.py", line 19, in -toplevel-
emails = re.findall('\S*\s(\w+@\w+\.\w+)', senders)
File
"/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/sre.py",
line 167, in findall
return _compile(pattern, flags).findall(string)
TypeError: expected string or buffer


any help would be appreciated.








[('460 (BODY[HEADER.FIELDS (FROM)] {46}', 'From: Kevin Feng
<[email protected]>\r\n\r\n'), ')', ('461 (BODY[HEADER.FIELDS
(FROM)] {37}', 'From: Kevin <[email protected]>\r\n\r\n'), ')']
 
F

Fredrik Lundh

Kun said:
i tried to parse (below) with the regular expression: emails =
re.findall('\S*\s(\w+@\w+\.\w+)', senders)

and got the following error:

Traceback (most recent call last):
File "/Life/School/Homework/Spring 2006/OPIM
399/Tutorial/IMAP/scannermailer.py", line 19, in -toplevel-
emails = re.findall('\S*\s(\w+@\w+\.\w+)', senders)
File
"/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/sre.py",
line 167, in findall
return _compile(pattern, flags).findall(string)
TypeError: expected string or buffer

any help would be appreciated.

findall expects a string as the target, not a list of strings. you can either
use a loop to apply the RE to each string fragment separately, or join the
fragments into a single string before you pass it to re.findall.

on the other hand, your data looks pretty regular. why not just loop over
the fragments and look for things that start with "From:" ?

</F>
 
P

Paul McGuire

Kun said:
i have a list of that is:

[('460 (BODY[HEADER.FIELDS (FROM)] {46}', 'From: Friend
<[email protected]>\r\n\r\n'), ')', ('462 (BODY[HEADER.FIELDS (FROM)] {37}',
'From: Kun <[email protected]>\r\n\r\n'), ')']


how do i parse the email addresses out of it into another list or string?

data = [('460 (BODY[HEADER.FIELDS (FROM)] {46}',
'From: Friend <[email protected]>\r\n\r\n'), ')',
('462 (BODY[HEADER.FIELDS (FROM)] {37}',
'From: Kun <[email protected]>\r\n\r\n'), ')']

for s in data:
if type(s) is tuple:
print s[1][6:].strip().split()[1]

Gives:
<[email protected]>
<[email protected]>


-- Paul
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,276
Latest member
Sawatmakal

Latest Threads

Top