[英]How can I use datetime.strptime on a string in the format '0000-00-00T00:00:00+00:00'?
[英]How do I remove the 00:00:00 from my string
我正在嘗試使用熊貓從鏈接中刪除00:00:00(HH:MM:SS)。
https://www.z-ratos.com/orical/?from=USD&amount=0&date=2018-10-24 00:00:00
因此,我如何從我的鏈接中刪除此00:00:00,以便其正常工作。 我嘗試了這段代碼:
import requests
import pandas as pd
from datetime import *
# TO TAKE DATE DIFFERENCE IN LIST
today = datetime.today()
dates = pd.date_range('2018-10-13', today)
#print(dates)
for i in dates:
#print(i)
url = 'https://www.z-ratos.com/orical/?from=USD&amount=0&date%s'%i
print(url)
輸出結果為:
https://www.z-ratos.com/orical/?from=USD&amount=0&date=2018-10-24 00:00:00
.....
......
......
所需的輸出是:
https://www.z-ratos.com/orical/?from=USD&amount=0&date=2018-10-24
所以請提前幫助謝謝...
使用strftime("%Y-%m-%d")
例如:
import requests
import pandas as pd
from datetime import *
# TO TAKE DATE DIFFERENCE IN LIST
today = datetime.today()
dates = pd.date_range('2018-10-13', today)
#print(dates)
for i in dates:
#print(i)
url = 'https://www.z-ratos.com/orical/?from=USD&amount=0&date%s'%i.strftime("%Y-%m-%d")
print(url)
對字符串使用DatetimeIndex.strftime
:
dates = pd.date_range('2018-10-13', today).strftime('%Y-%m-%d')
然后format
或f-strings
:
for i in dates:
#print(i)
url = 'https://www.z-ratos.com/orical/?from=USD&amount=0&date={}'.format(i)
#python 3.6+ solution
#url = f'https://www.z-ratos.com/orical/?from=USD&amount=0&date={i}'
print(url)
使用Datetime
對象的date
方法。 要從所有日期中刪除時間戳記:
dates = [d.date() for d in dates]
另外,當您遍歷日期時,只需使用i.date()
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.