讀書筆記: 精通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()