Python Parsing - Multiple Emails in One Text File
I receive similar emails from multiple senders and use regular expressions m
and n
below to pull out needed strings. That part is working fine.
Regular expression o
however is what has me confused. The text file I am reading is a combination of 9 email messages saved to one text file and opened in Python as a string. Original Sender (regex o
) occurs at the beginning of each new message in the file (9 times)
I want to write the same Original Sender after each CUSIP and NAME found until a different Original Sender is matched.
I am using xlwt3 and wincom32.
Sample from the text file with combined emails which is very standard:
--- Original Sender: TOM MADEUPNAME, SOME BANK, N. ---
----- Original Message -----
From: TOM MADEUPNAME (SOME BANK, N.)
To: BOB THISISMYEMAIL (XYZ INVESTMENTS, INC)
At: 8/31 8:53:25
**Offerings**
Mezz ReRemics
Cusip Description Original Current Cashflow Collat Offering
05531UAB6 BCAP 2009-RR5 1A2 18,745 18,745 Snr Sup Fxd 45.000
Prime/Alt-A Fixed
Cusip Description Original Current Cashflow Collat Offering
059487AE8 BOAA 2006-6 CB5 25,940 14,350 Seq Fxd 83.000
12544XAX3 CWHL 2007-9 A13 10,190 10,190 Ssnr Nas Fxd 92.500
17312XAJ3 CMSI 2007-4 1A9 2,871 2,741 Spr Snr Fxd 86.000
--- Original Sender: JOE MADEUPNAME, EUROPEAN BANK SECURI ---
----- Original Message -----
From: JOE MADEUPNAME (EUROPEAN BANK SECURI)
To: BOB THISISMYEMAIL (XYZ INVESTMENTS, INC)
At: 8/31 8:20:16
8-31-2011
Alt-A Fixed
Bond O/F C/F Cpn FICO CAL WALB 60+ Notes Offer
CSMC 06-9 7A1 25.00 11.97 L+45 728 26 578 35.21 FLT,AS,0.0% 50-00
LXS 07-10H 2A1 68.26 34.01 L+16 744 6 125 33.98 SS,9.57% 42-00
CSMC 06-7 9A1 15.00 7.81 L+30 688 5 198 46.46 SS,0.0% 29-16
Prime Hybrid
Bond O/F C/F Cpn FICO CAL WALB 60+ Notes Offer
SARM 05-18 6A1 14.56 6.01 2.58 730 46 432 15.87 SEA,SS,5/1,12.3% 78-00
Alt-A Hybrid
Bond O/F C/F Cpn FICO CAL WALB 60+ Notes Offer
ARMT 05-12 2A1 23.78 10.71 3.07 712 48 556 35.32 SS,5/1,4.9% *SOLD
Option Arm
Bond O/F C/F Cpn FICO CAL WALB 60+ Notes Offer
DBALT 07-OA4 1A1B 10.00 7.25 L+13 716 63 562 47.17 SS,OC,42.2% 64-16
-------------------开发者_开发问答-------------------------------------------------------------------
Updated - working
count_cusip = 0
count_name = 0
count_sender = 0
cur_sender = ''
for line in lines:
o = re.search(r"Original Sender:\s\b\w+\s\w+", line)
if o:
count_sender += 1
ws.write(count_sender,2,o.group(0))
ws.write(count_sender,2,cur_sender)
cur_sender = o.group(0)
m = re.search('[0-9]{3}[a-zA-Z0-9]{6}', line)
if m:
count_cusip += 1
ws.write(count_cusip,0,m.group(0))
ws.write(count_cusip,2,cur_sender)
n = re.search('[A-Z]{3,5}\s[0-9]{1,4}\D{1,3}\S{1,3}\s{1,2}\w+', line)
if n:
count_name += 1
ws.write(count_name,1,n.group(0))
ws.write(count_cusip,2,cur_sender)
o = re.search(r"Original Sender:\s\b\w+\s\w+", line)
if o:
cur_sender = o.group(0)
ws.write(count_name,2,cur_sender)
Updated Output - as desired.
CUSIP Bond Name Original Sender
00442PAD2 ACE 2006-OP1 A2B Original Sender: Nick Madeupname
12557YAE7 ARMT 05-12 2A1 Original Sender: Bobby Madeupname
39153VAT1 CSMC 06-9 7A1 Original Sender: Bobby Madeupname
05377RAE4 LXS 07-10H 2A1 Original Sender: Jane Madeupname
02005HAF0 CSMC 06-7 9A1 Original Sender: Jane Madeupname
Your question isn't completely clear since you didn't show an output sample, but here's an educated guess:
count_cusip = 0
count_name = 0
count_sender = 0
cur_sender = ''
for line in lines:
m = re.search('[0-9]{3}[a-zA-Z0-9]{6}', line)
if m:
count_cusip += 1
ws.write(count_cusip,0,m.group(0))
ws.write(count_cusip,2,cur_sender)
n = re.search('[A-Z]{3,5}\s[0-9]{1,4}\D{1,3}\S{1,3}\s{1,2}\w+', line)
if n:
count_name += 1
ws.write(count_name,1,n.group(0))
ws.write(count_name,2,cur_sender)
o = re.search(r"Original Sender:\s\b\w+\s\w+", line)
if o:
count_sender += 1
cur_sender = o.group(0)
Rather than writing the Original Sender when encountered, you need to save it and write the current value for each cusip and name.
Any chance you can change the format to something recognized by the mailbox
module in the standard library. Then you could let that module handle all the parsing.
精彩评论