如何按时间顺序重新排序mbox文件?

2024-10-01 09:16:39 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个单独的spool mbox文件,它是用evolution创建的,包含我希望打印的一系列电子邮件。我的问题是这些邮件没有按时间顺序放入mbox文件中。我想知道使用bash、perl或python将文件从头到尾排序的最佳方法。我想把收到的文件寄给我,并把我寄来的文件寄给我。使用maildir文件会更容易些吗?在

电子邮件当前的格式为:

From x@blah.com Fri Aug 12 09:34:09 2005
Message-ID: <42FBEE81.9090701@blah.com>
Date: Fri, 12 Aug 2005 09:34:09 +0900
From: me <x@blah.com>
User-Agent: Mozilla Thunderbird 1.0.6 (Windows/20050716)
X-Accept-Language: en-us, en
MIME-Version: 1.0
To: someone <someone@hotmail.com>
Subject: Re: (no subject)
References: <BAY101-F9353854000A4758A7E2CCA9BD0@phx.gbl>
In-Reply-To: <BAY101-F9353854000A4758A7E2CCA9BD0@phx.gbl>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 8bit
Status: RO
X-Status: 
X-Keywords:                 
X-UID: 371
X-Evolution-Source: imap://x+blah.com@blah.com/
X-Evolution: 00000002-0010

Hey

the actual content of the email

someone wrote:

> lines of quotedtext

我想知道是否有一种方法可以使用这些信息轻松地重新组织文件,也许是用perl或类似的方法。在


Tags: 文件to方法fromcom电子邮件augperl
3条回答

如果使用Thunderbird的ImportExportTools插件将邮件消息导入mbox,Python解决方案将无法工作。 存在一个错误:消息应以“from”行作为前缀,格式如下:

From - Tue Apr 27 19:42:22 2010

但是ImportExportTools的前缀是这样的“from”行:

^{2}$

所以有两个错误:

  1. “时间年”序列被打断 '年份时间'
  2. 带GMT的额外垃圾 信息和时区名称

因为Python的邮箱.py/unix邮箱具有硬编码的regexp用于“from”行匹配,某些消息无法分析。在

我给作者写了错误消息,但是有很多错误的导入消息:(。在

这就是在python中实现的方法:

#!/usr/bin/python2.5
from email.utils import parsedate
import mailbox

def extract_date(email):
    date = email.get('Date')
    return parsedate(date)

the_mailbox = mailbox.mbox('/path/to/mbox')
sorted_mails = sorted(the_mailbox, key=extract_date)
the_mailbox.update(enumerate(sorted_mails))
the_mailbox.flush()

重写mbox的意义是什么,而您可以在加载邮箱时重新排序内存中的邮件?你想订几点?接收日期?发送日期?不管怎样,所有使用mbox的Ruby/Python/Perl模块都可以做到这一点。在

相关问题 更多 >