所以我在我的 django 應用程式中進行了一些抓取。
我在抓取時得到的東西包含一個看起來像這樣的日期元素
7 april 2022 19:00 - 8 april 04:00 utc 02
我已經想出了如何拉開開始日和兩個月的時間,但是我在拉動軍事時間和第二天時遇到了一些麻煩。
我在想我%在搜索軍事時間時必須以某種方式使用運算子,但在網上找不到太多,而且自從我使用 python 以來已經有一段時間了。
對于第二天的拉動,我嘗試做與第一天相同的事情,但問題是我最終"02"從"utc 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
uj5u.com熱心網友回復:
您可以使用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)
編輯將開始年份轉移到結束年份
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/456710.html
下一篇:根據匹配的正則運算式創建列值
