[Python]Regex–簡單的郵件處理

來源:互聯網
上載者:User
 讀書筆記: 精通Regex

txt檔案

#mail.txt From Xihaode o dfsfsa Received: womenshiw To: xiaopengyou@126.com (Xiao Peng)From: dapengyou@qq.com (Da Peng)Date: 05/02/2000 at 04:58:50Subject: Re: Using the mod() function with negative numbersThanks very much for sorting this out. I had worked out how it was arriving at the result, but did not know whether it was correct. We had also tried it in Lotus 1-2-3 and the result given was -40, which was what the user expected. Nice to have built some user confidence in Excel.HiHi 
代碼

#mail.py python2.7#用Regex處理郵件'''tip:1 直接讀取檔案的時候總是會在每一行後面加一個'\n'的分行符號號,可以用rstrip處理下2 頭資訊的取值用正則就行了'''import re try:     f = open('mail.txt','r')except IOError ,e:    e.messageflag = 1text = ''textT = '' reDate = r'^Date:(.*)' reFrom = r'^From:(.*)'reTo = r'^To:(.*)\s*\((.*)\)'sendtoName = ''for line in f:    line=line.rstrip('\n')    if line and flag:     #對資訊處理 找到時間 發信和接收人        if re.match(reDate,line,re.I):            print line         elif re.match(reFrom,line,re.I):            print line         elif re.match(reTo,line,re.I):            print line             sendto = re.findall(reTo,line,re.I)            sendtoName = sendto[0][1]     elif (not line) and flag:                 flag=0           #當出現空行之後flag變成0,把本文後面的部分都放到text中    else:        text+=line+'\n'print '\nHi ,',sendtoName,':'        print textf.close()
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.