如何在 python 中搜索军事时间第一次和第二次出现的字符串
How to search a string for the first and second occurance of military time in python
所以我正在我的 Django 应用程序中进行一些抓取。
我在抓取时得到的东西包含一个看起来像这样的日期元素
7 april 2022 19:00 - 8 april 04:00 utc+02
我已经想出了如何提取开始日和两个月的方法,但是我在提取军用时间和第二天时遇到了一些麻烦。
我想我在搜索军事时间时必须以某种方式使用 %
运算符,但在网上找不到太多,而且我使用 python 已经有一段时间了.
对于第二天的拉动,我尝试做与第一次拉动时相同的事情,但问题是我最终从 "utc+02"
拉动了 "02"
。
感谢您的帮助!
time = temp[0] # the content of scraping
emp_str = ""
f_digit = False
for d in time: # finds the first digit(s) aka the day
if d.isdigit():
emp_str = emp_str+d
f_digit = True
elif f_digit:
break
s_day = emp_str
emp_ar = []
s_month = ""
e_month = ""
for m in MONTHS: # this finds the months in the string and adds their index for comparing MONTHS contains the months aka apr jun jul and so on
index = time.find(m)
if index != -1:
emp_ar.append(m)
emp_ar.append(index)
if len(emp_ar) > 2:
print("happened")
# checks which index is bigger aka which one is mentioned first
if int(emp_ar[1]) < int(emp_ar[3]):
s_month = emp_ar[0]
e_month = emp_ar[2]
else:
s_month = emp_ar[2]
e_month = emp_ar[0]
elif len(emp_ar) > 0:
s_month = emp_ar[0]
e_month = emp_ar[0]
# puts the month in s/e month for start/end
您可以使用datetime.datetime.strptime()方法。
在注释中,格式似乎已更改,但如果脱离原始格式,您的代码必须如下所示:
from datetime import datetime
s = '7 april 2022 19:00 - 8 april 04:00 utc+02'
s = s.split('-')
startdate = datetime.strptime(s[0].strip(), '%d %B %Y %H:%M')
enddate = datetime.strptime(s[1].strip()+'00', '%d %B %H:%M utc%z')
startdate = startdate.replace(tzinfo=enddate.tzinfo)
enddate = enddate.replace(year=startdate.year)
编辑以将开始年份转移到结束年份
所以我正在我的 Django 应用程序中进行一些抓取。
我在抓取时得到的东西包含一个看起来像这样的日期元素
7 april 2022 19:00 - 8 april 04:00 utc+02
我已经想出了如何提取开始日和两个月的方法,但是我在提取军用时间和第二天时遇到了一些麻烦。
我想我在搜索军事时间时必须以某种方式使用 %
运算符,但在网上找不到太多,而且我使用 python 已经有一段时间了.
对于第二天的拉动,我尝试做与第一次拉动时相同的事情,但问题是我最终从 "utc+02"
拉动了 "02"
。
感谢您的帮助!
time = temp[0] # the content of scraping
emp_str = ""
f_digit = False
for d in time: # finds the first digit(s) aka the day
if d.isdigit():
emp_str = emp_str+d
f_digit = True
elif f_digit:
break
s_day = emp_str
emp_ar = []
s_month = ""
e_month = ""
for m in MONTHS: # this finds the months in the string and adds their index for comparing MONTHS contains the months aka apr jun jul and so on
index = time.find(m)
if index != -1:
emp_ar.append(m)
emp_ar.append(index)
if len(emp_ar) > 2:
print("happened")
# checks which index is bigger aka which one is mentioned first
if int(emp_ar[1]) < int(emp_ar[3]):
s_month = emp_ar[0]
e_month = emp_ar[2]
else:
s_month = emp_ar[2]
e_month = emp_ar[0]
elif len(emp_ar) > 0:
s_month = emp_ar[0]
e_month = emp_ar[0]
# puts the month in s/e month for start/end
您可以使用datetime.datetime.strptime()方法。
在注释中,格式似乎已更改,但如果脱离原始格式,您的代码必须如下所示:
from datetime import datetime
s = '7 april 2022 19:00 - 8 april 04:00 utc+02'
s = s.split('-')
startdate = datetime.strptime(s[0].strip(), '%d %B %Y %H:%M')
enddate = datetime.strptime(s[1].strip()+'00', '%d %B %H:%M utc%z')
startdate = startdate.replace(tzinfo=enddate.tzinfo)
enddate = enddate.replace(year=startdate.year)
编辑以将开始年份转移到结束年份