我从电子邮件正文中提取了这些数据
Data=("""-------- Forwarded Message --------
Subject: Sample Report
Date: Thu, 6 Apr 2017 16:39:19 +0000
From: test1@abc.com
To: test2@xyz.com""")
我想提取这个特定的日期和月份,并将其复制到变量
中需要输出
Date = 6
Month = "Apr"
任何人都可以使用正则表达式来帮助解决这个问题吗?
答案 0 :(得分:0)
您可以使用正则表达式提取日期
<强>实施例强>
import re
from dateutil.parser import parse
s = """-------- Forwarded Message --------
Subject: Sample Report
Date: Thu, 6 Apr 2017 16:39:19 +0000
From: test1@abc.com
To: test2@xyz.com"""
date = re.search("Date(.*)$", s, re.MULTILINE)
if date:
date = date.group().replace("Date:", "").strip()
d = parse(date)
Date = d.day
Month = d.strftime("%b")
print(Date, Month)
<强>输出:强>
6 Apr
答案 1 :(得分:0)
您可以将此正则表达式用于多行模式m
:
^Date:[^,]+,\ (\d+) (\w+)
这将分别捕获第1组和第2组中的日期和月份,因此可以轻松地将匹配解压缩为两个变量,如下所示:
date, month = re.search("^Date:[^,]+,\ (\d+) (\w+)", Data, re.MULTILINE).groups()
date = int(date)
print(date, month)
# output: 6 Apr
答案 2 :(得分:0)
添加@Rakesh的解决方案,
import re
from datetime import datetime
data1 = re.sub(' ', '', data)
res = re.search(r'Date(.*)$', data1, re.MULTILINE).group()
res2 = datetime.strptime(res, 'Date:%a,%d%b%Y%X%z')
print(res2.day, res2.month)