如何按时间顺序重新排序一个mbox文件? [英] How can I reorder an mbox file chronologically?
问题描述
我有一个用进化创建的一个spool mbox文件,其中包含一些我想打印的电子邮件。我的问题是电子邮件没有按时间顺序放入mbox文件。我想知道使用bash,perl或python从头到尾排列文件的最佳方法。我想通过收到的文件发给我,并发送给我发送的文件。电子邮件的存在格式如下:
从x@blah.com Fri 8月12日09:34:09 2005
Message-ID:< 42FBEE81.9090701@blah.com>
日期:2005年8月12日,星期五09:34:09 +0900
从:我< x@blah.com>
用户代理:Mozilla Thunderbird 1.0.6(Windows / 20050716)
X-Accept-Language:en-us,en
MIME版本:1.0
To:someone< ; someone@hotmail.com>
主题:Re:(无主题)
参考文献:< BAY101-F9353854000A4758A7E2CCA9BD0@phx.gbl>
回覆者:< BAY101-F9353854000A4758A7E2CCA9BD0@phx.gbl>
Content-Type:text / plain;字符集= ISO-8859-1; format = flowed
Content-Transfer-Encoding:8bit
状态:RO
X状态:
X关键字:
X-UID:371
X-Evolution-Source:imap://x+blah.com@blah.com/
X-Evolution:00000002-0010
嘿
实际电子邮件的内容
有人写道:
>引用的行
我想知道是否有方法使用这些信息轻松地重组文件,可能用perl等等。
这是你可以在python中执行的操作:
#!/ usr / bin / python2.5
pre>
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(枚举(sorted_mails))
the_mailbox.flush()
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 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
I am wondering if there is a way to use this information to easily reorganize the file, perhaps with perl or such.
解决方案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()
这篇关于如何按时间顺序重新排序一个mbox文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!