newbie parsing question

Discussion in 'Python' started by Kun, Mar 25, 2006.

  1. Kun

    Kun Guest

    i have a list of that is:

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


    how do i parse the email addresses out of it into another list or string?
    Kun, Mar 25, 2006
    #1
    1. Advertising

  2. Kun

    Guest

    , Mar 25, 2006
    #2
    1. Advertising

  3. Kun

    Kun Guest

    wrote:
    > Use Regular expressions
    > http://docs.python.org/lib/module-re.html
    >

    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
    <>\r\n\r\n'), ')', ('461 (BODY[HEADER.FIELDS
    (FROM)] {37}', 'From: Kevin <>\r\n\r\n'), ')']
    Kun, Mar 25, 2006
    #3
  4. "Kun" wrote:

    > 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>
    Fredrik Lundh, Mar 25, 2006
    #4
  5. Kun

    Paul McGuire Guest

    "Kun" <> wrote in message
    news:e022qp$eou1$...
    > i have a list of that is:
    >
    > [('460 (BODY[HEADER.FIELDS (FROM)] {46}', 'From: Friend
    > <>\r\n\r\n'), ')', ('462 (BODY[HEADER.FIELDS (FROM)] {37}',
    > 'From: Kun <>\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 <>\r\n\r\n'), ')',
    ('462 (BODY[HEADER.FIELDS (FROM)] {37}',
    'From: Kun <>\r\n\r\n'), ')']

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

    Gives:
    <>
    <>


    -- Paul
    Paul McGuire, Mar 25, 2006
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. GIMME
    Replies:
    2
    Views:
    873
    GIMME
    Feb 11, 2004
  2. Naren
    Replies:
    0
    Views:
    577
    Naren
    May 11, 2004
  3. Christopher Diggins
    Replies:
    0
    Views:
    607
    Christopher Diggins
    Jul 9, 2007
  4. Christopher Diggins
    Replies:
    0
    Views:
    430
    Christopher Diggins
    Jul 9, 2007
  5. John Levine
    Replies:
    0
    Views:
    727
    John Levine
    Feb 2, 2012
Loading...

Share This Page