2

我是一个尝试从电子邮件标题中提取数据的 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 行)的“发件人:”字段中有换行符之外,这个输出会很好,所以我错过了“nobody@hotmail.com”。

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

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

4

2 回答 2

1

您的演示文本实际上是 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)
于 2013-02-24T18:58:34.497 回答
0

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

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

于 2013-02-24T17:54:55.640 回答