python to extract list match from email thread
<p>I m new to python. I need to retrieve the list of match </p>

<p>for Example my text is below which is an email.
I need to extract all To, From, Sent, Subject and body from a mail thread.</p>

<p>Result need to From List</p>

<p>From(1) = Crandall, Sean 
From(2) = Nettelton, Marcus  </p>

<p>To(1)= Crandall, Sean; Badeer, Robert
To(2)= Meredith, Kevin</p>

<p>Like for above Sent, subject etc</p>

<pre><code>"-----Original Message-----
From:   Crandall, Sean  
Sent:   Wednesday, May 23, 2001 2:56 PM
To: Meredith, Kevin
Subject:    RE: Spreads and Product long desc.

Kevin,

Is the SP and NP language in the spread language the same language we use when we transact SP15 or NP15 on eol?

 -----Original Message-----
From:   Meredith, Kevin  
Sent:   Wednesday, May 23, 2001 11:16 AM
To: Crandall, Sean; Badeer, Robert
Subject:    FW: Spreads and Product long desc."
</code></pre>

<p>If you don't know how to use regex and as your problem is not that tough, you may consider to use the <code>split()</code> and <code>replace()</code> functions.</p>

<p>Here are some lines of code that might be a good start:</p>

<pre><code>mails = """-----Original Message-----
From:   Crandall, Sean  
Sent:   Wednesday, May 23, 2001 2:56 PM
To: Meredith, Kevin
Subject:    RE: Spreads and Product long desc.

Kevin,

Is the SP and NP language in the spread language the same language we use when we transact SP15 or NP15 on eol?

 -----Original Message-----
From:   Meredith, Kevin  
Sent:   Wednesday, May 23, 2001 11:16 AM
To: Crandall, Sean; Badeer, Robert
Subject:    FW: Spreads and Product long desc."""

mails_list = mails.split("-----Original Message-----\n")
mails_from = []
mails_sent = []
mails_to = []
mails_subject = []
mails_body = []

for mail in mails_list:
    if not mail:
        continue
    inter = mail.split("From:   ")[1].split("\nSent:   ")
    mails_from.append(inter[0])
    inter = inter[1].split("\nTo: ")
    mails_sent.append(inter[0])
    inter = inter[1].split("\nSubject:    ")
    mails_to.append(inter[0])
    inter = inter[1].split("\n")
    mails_subject.append(inter[0])
    mails_body.append(inter[0])
</code></pre>

<p>See how this only use really basic concepts.</p>

<p>Here are some points that you might need to consider:</p>

<ul>
<li>Try by yourself, you might need some adjustments.</li>
<li>With that method, the parsing method is quite tough, the format of the mails must be really accurate.</li>
<li>There might be some space that you want to remove, for example with the <code>replace()</code> method.</li>
</ul>

python regex match