我试图从这个字符串中提取开始日期和时间以及结束日期和时间,但不知道该怎么做,我的第一个虽然是找到“开始时间和日期”,然后获得下一个20 chars从这一点开始尝试将它们转换为日期和时间之后,但是我认为有更好的方法可以做到这一点吗?
body = """
Dear Customer,
Details of service affected:
Start Time & Date: 02/12/2016 02:00
Completion Time & Date: 02/12/2016 06:00
Details of Work:
Change faulty card
"""
if "Start Time & Date" in body:
#get next 20 chars?
答案 0 :(得分:2)
尝试regex
:
>>> import re
>>>
>>> re.findall('\d{2}/\d{2}/\d{4} \d{2}:\d{2}', body)
['02/12/2016 02:00', '02/12/2016 06:00']
答案 1 :(得分:-1)
import re
body = """
Dear Customer,
Details of service affected:
Start Time & Date: 02/12/2016 02:00
Completion Time & Date: 02/12/2016 06:00
Details of Work:
Change faulty card
"""
my_time = re.search(r'\d{2}/\d{2}/\d{4} \d{2}:\d{2}', body)
print (my_time.group())