我必须从python中的字符串中以所需的格式提取开始日期,开始时间和结束日期,结束时间。为此,我写了一个代码:
from datetime import *
#date_time = 'Sun Apr 22 2018 at 02:00 pm to Sun May 13 2018 at 02:00 pm'
#date_time = 'Sun Apr 22 2018 at 02:00 pm'
#date_time = 'Sat Apr 28 2018 at 05:00 pm to 07:00 am'
#date_time = 'Fri Oct 15 2021'
date_time = 'Sat Aug 01 2020'
#date_time = 'Wed Jan 22 2020 at 01:30 pm'
#date_time = 'Wed Jan 01 2020 to Thu Jan 02 2020'
delim = 'to'
if delim in date_time:
start_date_time = date_time.split(delim, 2)[0]
start_date_time = start_date_time.partition(' ')[2] #remove the first word of the string
end_date_time = date_time.split(delim, 2)[1]
end_date_time = end_date_time.strip() #remove the first space in the string
#end_date_time = end_date_time.partition(' ')[2] #remove the first word of the string
delim = 'at'
start_date = start_date_time.split(delim, 2)[0]
start_date = start_date.rstrip() #remove the space at the end of the string
start_date = datetime.strptime(start_date, "%b %d %Y")
start_time = start_date_time.split(delim, 2)[1]
start_time = start_time.strip()
start_time = datetime.strptime(start_time, "%I:%M %p")
start_time = datetime.strftime(start_time, "%H:%M:%S")
if delim in end_date_time:
end_date_time = end_date_time.partition(' ')[2] #remove the first word of the string
end_date = end_date_time.split(delim, 2)[0]
#print(end_date)
end_date = end_date.rstrip()
end_date = datetime.strptime(end_date, "%b %d %Y")
#print(end_date)
end_time = end_date_time.split(delim, 2)[1]
end_time = end_time.strip()
end_time = datetime.strptime(end_time, "%I:%M %p")
end_time = datetime.strftime(end_time, "%H:%M:%S")
else:
end_date = start_date
end_time = end_date_time
end_time = end_time.strip()
end_time = end_time.rstrip()
end_time = datetime.strptime(end_time, "%I:%M %p")
end_time = datetime.strftime(end_time, "%H:%M:%S")
else:
delim = 'at'
if delim in date_time:
start_date_time = date_time.partition(' ')[2]
#end_date_time = start_date_time
#delim = 'at'
start_date = start_date_time.split(delim, 2)[0]
start_date = start_date.rstrip()
start_date = datetime.strptime(start_date, "%b %d %Y")
start_time = start_date_time.split(delim, 2)[1]
start_time = start_time.strip()
start_time = datetime.strptime(start_time, "%I:%M %p")
start_time = datetime.strftime(start_time, "%H:%M:%S")
#end_date = end_date_time.split(delim, 2)[0]
#print(end_date)
#end_date = end_date.rstrip()
#end_date = datetime.strptime(end_date, "%b %d %Y")
#print(end_date)
end_date = start_date
end_time = ''
else:
start_date_time = date_time.partition(' ')[2]
#end_date_time = start_date_time
#start_date = start_date_time.split(delim, 2)[0]
start_date = start_date_time.rstrip()
start_date = datetime.strptime(start_date, "%b %d %Y")
start_time = ''
end_date = start_date
end_time = ''
'''delim = 'at'
start_date = start_date_time.split(delim, 2)[0]
start_date = datetime.strptime(start_date, "%b %d %Y ")
start_time = start_date_time.split(delim, 2)[1]
start_time = start_time.strip()
start_time = datetime.strptime(start_time, "%I:%M %p")
start_time = datetime.strftime(start_time, "%H:%M:%S")
end_date = end_date_time.split(delim, 2)[0]
end_date = datetime.strptime(end_date, "%b %d %Y ")
end_time = end_date_time.split(delim, 2)[1]
end_time = end_time.strip()
end_time = datetime.strptime(end_time, "%I:%M %p")
end_time = datetime.strftime(end_time, "%H:%M:%S")'''
#print(start_date_time)
print(start_date)
print(start_time)
print('-'*40)
#print(end_date_time)
print(end_date)
print(end_time)
对于date_time = 'Fri Oct 15 2021'
,它产生的输出正确为:
$ python date_time.py
2021-10-15 00:00:00
----------------------------------------
2021-10-15 00:00:00
但对于另一个类似的字符串date_time = 'Sat Aug 01 2020'
,它会产生错误:
$ python date_time.py
Traceback (most recent call last):
File "date_time.py", line 69, in <module>
start_time = start_date_time.split(delim, 2)[1]
IndexError: list index out of range
我无法调试代码。