如何从字符串中删除00:00:00

时间:2018-10-24 11:41:36

标签: python string pandas date

我正在尝试使用熊猫从链接中删除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

所以请帮忙 在此先感谢...

3 个答案:

答案 0 :(得分:6)

使用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)

答案 1 :(得分:3)

DatetimeIndex.strftime用于字符串:

dates = pd.date_range('2018-10-13', today).strftime('%Y-%m-%d')

然后是formatf-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)

答案 2 :(得分:1)

使用date对象的Datetime方法。要从所有日期中删除时间戳,请执行以下操作:

dates = [d.date() for d in dates]

或者,当您遍历日期时,只需使用i.date()