简体   繁体   中英

List of dates, Python - [AttributeError: 'list' object has no attribute 'strftime']

Trying to create a list of dates in python, going back 10 days. When I try to format the variables to string I get the error -

"AttributeError: 'list' object has no attribute 'strftime'"

Is there a method I can use to create this variable and convert to the list to string so I can use it in my code?

import datetime 
url_dat= []
x = 0
for value in range(10):
    x += 1
    url_dat.append(datetime.datetime.today() - datetime.timedelta(days=x))
    for d in url_dat:
        url_dat.strftime("%Y-%m-%dT00:00:00.000")

print(url_dat)

try this:

>>>for value in range(10):
       x += 1
       url_dat.append(datetime.datetime.today() - datetime.timedelta(days=x))
       for d in url_dat:
           d.strftime("%Y-%m-%dT00:00:00.000")
    
>>>print(url_dat)

This is the final working code:

def urls():
    dat= []
    dat_str = []
    url_lst = []

    x = 0
    for val in range(7):
        x += 1
        dat.append(datetime.datetime.today() - datetime.timedelta(days=x))

    for d in dat:
        dat_str.append(d.strftime("%Y-%m-%dT00:00:00.000"))

    y = 0
    for u in dat_str:
        url_lst.append('https://health.data.ny.gov/resource/xdss-u53e.json?test_date=' + dat_str[y])
        y += 1
    return(url_lst)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM