newbie parsing question

Fredrik Lundh fredrik at pythonware.com
Sat Mar 25 01:13:57 EST 2006


"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>






More information about the Python-list mailing list