我是python的新手。我需要检索匹配列表
例如我的文字在下面是一封电子邮件。 我需要从邮件线程中提取所有To,From,Sent,Subject和body。
结果需要从列表
来自(1)= Crandall,肖恩 从(2)= Nettelton,Marcus
To(1)= Crandall,Sean;巴德尔,罗伯特 致(2)=梅雷迪思,凯文
喜欢以上发送,主题等
"-----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."
答案 0 :(得分:1)
您可以使用re.findall()
,请参阅:https://docs.python.org/2/library/re.html#re.findall。 E.g。
re.findall("From: (.*) ", input_string);
会返回一个From-names(['Crandall, Sean', 'Meredith, Kevin']
)列表,假设它总是相同数量的空格。
如果你想获得幻想,可以在同一个表达式中进行多次搜索:例如
re.findall("From: (.*) \nSent: (.*)", input_string);
会返回[('Crandall, Sean', 'Wednesday, May 23, 2001 2:56 PM'), ('Meredith, Kevin', 'Wednesday, May 23, 2001 11:16 AM')]
答案 1 :(得分:1)
如果您不知道如何使用正则表达式,并且您的问题并不严重,则可以考虑使用split()
和replace()
函数。
以下是一些可能是一个良好开端的代码行:
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])
了解这只是如何使用真正的基本概念。
以下是您可能需要考虑的一些要点:
replace()
方法。