E-Mail Parsing

Matt Nordhoff mnordhoff at mattnordhoff.com
Thu Dec 13 02:29:47 EST 2007


Merrigan wrote:
> I am writing a script to administer my E-Mail Server. The One thing
> I'm currently struggling with is kind of Parsing the E-Mail adress
> that I supply to the script.
> 
> I need to get the username (The part BEFORE the @ sign) out of the
> address so that I can use it elsewhere. The problem I have with this
> is that both the domain, and the username are variable lenghts and
> that I have no idea how to split the two parts.
> 
> Is there any way that I can do this?
> 
> Thank ye very much.

>>> addr = "user at example.com"
>>> addr.split("@")
['user', 'example.com']

If it's formatted like a To header ("User <user at example.com>"), use
email.Utils.parseaddr() to get the address out of it.\

The email modules might help you a lot:

<http://docs.python.org/lib/module-email.html>
-- 



More information about the Python-list mailing list