在python中使用正则表达式解析电子邮件标头

时间:2013-02-24 17:38:33

标签: python regex email-parsing

我是一个尝试从电子邮件标头中提取数据的python初学者。我在一个文本文件中有成千上万的电子邮件,并且我希望从每封邮件中提取发件人的地址,收件人地址和日期,并将其写入新文件中的单个分号分隔行。

这很难看,但这就是我想出来的:

import re

emails = open("demo_text.txt","r") #opens the file to analyze
results = open("results.txt","w") #creates new file for search results

resultsList = []

for line in emails:
    if "From - " in line: #recgonizes the beginning of a email message and adds a linebreak
        newMessage = re.findall(r'\w\w\w\s\w\w\w.*', line)
        if newMessage:
            resultsList.append("\n")        
    if "From: " in line:
        address = re.findall(r'[\w.-]+@[\w.-]+', line)
        if address:
            resultsList.append(address)
            resultsList.append(";")
    if "To: " in line:
        if "Delivered-To:" not in line: #avoids confusion with 'Delivered-To:' tag
            address = re.findall(r'[\w.-]+@[\w.-]+', line)
            if address:
                for person in address:
                    resultsList.append(person)
                    resultsList.append(";")
    if "Date: " in line: 
            date = re.findall(r'\w\w\w\,.*', line)
            resultsList.append(date)
            resultsList.append(";")

for result in resultsList:
    results.writelines(result)


emails.close()
results.close()

这是我的'demo_text.txt':

From - Sun Jan 06 19:08:49 2013
X-Mozilla-Status: 0001
X-Mozilla-Status2: 00000000
Delivered-To: somebody_1@hotmail.com
Received: by 10.48.48.3 with SMTP id v3cs417003nfv;
        Mon, 15 Jan 2007 10:14:19 -0800 (PST)
Received: by 10.65.211.13 with SMTP id n13mr5741660qbq.1168884841872;
        Mon, 15 Jan 2007 10:14:01 -0800 (PST)
Return-Path: <nobody@hotmail.com>
Received: from bay0-omc3-s21.bay0.hotmail.com (bay0-omc3-s21.bay0.hotmail.com [65.54.246.221])
        by mx.google.com with ESMTP id e13si6347910qbe.2007.01.15.10.13.58;
        Mon, 15 Jan 2007 10:14:01 -0800 (PST)
Received-SPF: pass (google.com: domain of nobody@hotmail.com designates 65.54.246.221 as permitted sender)
Received: from hotmail.com ([65.54.250.22]) by bay0-omc3-s21.bay0.hotmail.com with Microsoft SMTPSVC(6.0.3790.2668);
         Mon, 15 Jan 2007 10:13:48 -0800
Received: from mail pickup service by hotmail.com with Microsoft SMTPSVC;
         Mon, 15 Jan 2007 10:13:47 -0800
Message-ID: <BAY115-F12E4E575FF2272CF577605A1B50@phx.gbl>
Received: from 65.54.250.200 by by115fd.bay115.hotmail.msn.com with HTTP;
        Mon, 15 Jan 2007 18:13:43 GMT
X-Originating-IP: [200.122.47.165]
X-Originating-Email: [nobody@hotmail.com]
X-Sender: nobody@hotmail.com
From: =?iso-8859-1?B?UGF1bGEgTWFy7WEgTGlkaWEgRmxvcmVuemE=?=
 <nobody@hotmail.com>
To: somebody_1@hotmail.com, somebody_2@gmail.com, 3_nobodies@yahoo.com.ar
Bcc: 
Subject: fotos
Date: Mon, 15 Jan 2007 18:13:43 +0000
Mime-Version: 1.0
Content-Type: multipart/mixed; boundary="----=_NextPart_000_d98_1c4f_3aa9"
X-OriginalArrivalTime: 15 Jan 2007 18:13:47.0572 (UTC) FILETIME=[E68D4740:01C738D0]
Return-Path: nobody@hotmail.com

输出结果为:

somebody_1@hotmail.com;somebody_2@gmail.com;3_nobodies@yahoo.com.ar;Mon, 15 Jan 2007 18:13:43 +0000;

这个输出没问题,只是在我的demo_text.txt(第24行)的'From:'字段中有换行符,所以我想念'nobody@hotmail.com'。

我不知道如何告诉我的代码跳过换行符,仍然可以在From:标签中找到电子邮件地址。

更一般地说,我确信有更多合理的方法来完成这项任务。如果有人能指出我正确的方向,我一定会很感激。

2 个答案:

答案 0 :(得分:0)

为了跳过换行符,您无法逐行阅读。您可以尝试加载文件,并使用关键字(From,To等)作为边界。因此,当您搜索“从 - ”时,您将其余的关键字用作边界,这样它们就不会包含在列表的部分中。

另外,提到这个原因你说你是初学者: 命名非类变量的“Pythonic”方法是使用下划线。所以resultsList应该是results_list。

答案 1 :(得分:0)

您的演示文稿实际上是mbox格式,可以使用mailbox模块中的相应对象进行完美处理:

from mailbox import mbox
import re

PAT_EMAIL = re.compile(r"[0-9A-Za-z._-]+\@[0-9A-Za-z._-]+")

mymbox = mbox("demo.txt")
for email in mymbox.values():
    from_address = PAT_EMAIL.findall(email["from"])
    to_address = PAT_EMAIL.findall(email["to"])
    date = [ email["date"], ]
    print ";".join(from_address + to_address + date)