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

发布于 2024-07-10 12:32:19 字数 1947 浏览 11 评论 0原文

我有一个使用进化创建的单个线轴 mbox 文件,其中包含我希望打印的一系列电子邮件。 我的问题是电子邮件没有按时间顺序放入 mbox 文件中。 我想知道使用 bash、perl 或 python 从头到尾对文件进行排序的最佳方法。 对于发给我的文件,我想按已收到的方式订购;对于我发送的文件,我想按已发送的方式订购。 使用 maildir 文件或类似文件可能会更容易吗?

电子邮件目前的格式如下:

From [email protected] Fri Aug 12 09:34:09 2005
Message-ID: <[email protected]>
Date: Fri, 12 Aug 2005 09:34:09 +0900
From: me <[email protected]>
User-Agent: Mozilla Thunderbird 1.0.6 (Windows/20050716)
X-Accept-Language: en-us, en
MIME-Version: 1.0
To: someone <[email protected]>
Subject: Re: (no subject)
References: <[email protected]>
In-Reply-To: <[email protected]>
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://[email protected]/
X-Evolution: 00000002-0010

Hey

the actual content of the email

someone wrote:

> lines of quotedtext

我想知道是否有一种方法可以使用此信息轻松地重新组织文件,也许可以使用 perl 等。

I have a single spool mbox file that was created with evolution, containing a selection of emails that I wish to print. My problem is that the emails are not placed into the mbox file chronologically. I would like to know the best way to place order the files from first to last using bash, perl or python. I would like to oder by received for files addressed to me, and sent for files sent by me. Would it perhaps be easier to use maildir files or such?

The emails currently exist in the format:

From [email protected] Fri Aug 12 09:34:09 2005
Message-ID: <[email protected]>
Date: Fri, 12 Aug 2005 09:34:09 +0900
From: me <[email protected]>
User-Agent: Mozilla Thunderbird 1.0.6 (Windows/20050716)
X-Accept-Language: en-us, en
MIME-Version: 1.0
To: someone <[email protected]>
Subject: Re: (no subject)
References: <[email protected]>
In-Reply-To: <[email protected]>
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://[email protected]/
X-Evolution: 00000002-0010

Hey

the actual content of the email

someone wrote:

> lines of quotedtext

I am wondering if there is a way to use this information to easily reorganize the file, perhaps with perl or such.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

红玫瑰 2024-07-17 12:32:19

在 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()

This is how you could do it in 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()
余罪 2024-07-17 12:32:19

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

From - Tue Apr 27 19:42:22 2010

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

From - Sat May 01 2010 15:07:31 GMT+0400 (Russian Daylight Time)

因此存在两个错误:

  1. 序列“时间年份”分解为
    GMT 的“年时间”
  2. 额外垃圾
    信息以及时区名称

由于Python的mailbox.py/UnixMailbox已经硬编码了正则表达式来匹配'from'行,所以一些消息无法解析。

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

Python solution wont work if mail messages was imported into mbox using Thunderbird's ImportExportTools addon.
There are a bug: messages shall prefix with 'from' line in format:

From - Tue Apr 27 19:42:22 2010

but ImportExportTools prefix with such 'from' line:

From - Sat May 01 2010 15:07:31 GMT+0400 (Russian Daylight Time)

So there are two errors:

  1. sequence 'time year' broken into
    'year time'
  2. extra trash with GMT
    info along with time zone name

Since Python's mailbox.py/UnixMailbox has hardcoded regexp for 'from' line matching, some of messages can't parsed.

I wrote the error message to the author, but there are many mistakenly imported messages :(.

四叶草在未来唯美盛开 2024-07-17 12:32:19

当加载邮箱时可以对内存中的邮件重新排序,重写 mbox 有什么意义? 您想在哪一个时间订购? 收到日期? 发送日期? 无论如何,所有用于使用 mbox 的 Ruby/Python/Perl 模块都可以做到这一点。

What's the point in rewriting the mbox whereas you can reorder the mails in memory when loading up the mailbox? Which time is the one you want to order on? Receive date? Sent date? Anyway, all the Ruby/Python/Perl modules for playing with mboxes can do that.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文