Skip to main content
 首页 » 编程设计

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

2025年05月04日30开发

我是一名 Python 初学者,试图从电子邮件 header 中提取数据。我在一个文本文件中有数千封电子邮件,我想从每封邮件中提取发件人地址、收件人地址和日期,并将其写入新文件中以分号分隔的单个行。

这很丑陋,但这是我想出的:

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 行)的“发件人:”字段中有一个换行符外,此输出会很好,所以我错过了“nobody@hotmail.com”。

我不确定如何让我的代码跳过换行符并仍然在 From: 标记中找到电子邮件地址。

更一般地说,我确信有很多更明智的方法来完成这项任务。如果有人能指出我正确的方向,我将不胜感激。

请您参考如下方法:

您的演示文本实际上是 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)