gpt4 book ai didi

Python:如何在汽车列表中存储日期范围的结果

转载 作者:行者123 更新时间:2023-11-28 20:59:04 24 4
gpt4 key购买 nike

import datetime
import re
import pymongo
from datetime import timedelta, date

def daterange(d, d1):
for n in range(int ((d1 - d).days)):
yield d + timedelta(n)

#conect bd
uri = "mongodb://127.0.0.1:27017"
client = pymongo.MongoClient(uri)
database = client['db']
collection = database['currency']


d = input('Insert beginning date (yyyy-mm-dd): ')
d1 = input('Insert end date (yyyy-mm-dd): ')

#search bd
item = collection.find_one({"date" : d})
item1 = collection.find_one({"date" : d1})
d = collection.find_one({})
d1 = collection.find_one({})
datas = item['date']
datas1 = item1['date']

#conversão de string para objecto
dataObject = datetime.datetime.strptime(datas, "%Y-%m-%d")
dataObject1 = datetime.datetime.strptime(datas1, "%Y-%m-%d")

#range
for single_date in daterange(dataObject, dataObject1):
print (single_date.strftime("%Y-%m-%d"))

如果可能的话,我想将此代码的输出放入一个 var 列表中。例如,开始日期为 2018-05-07,结束日期为 2018-05-11,输出为:

2018-05-07
2018-05-08
2018-05-09
2018-05-10

如何将它放入变量中?

最佳答案

假设这部分正在生成列表中所需的日期:

#range
for single_date in daterange(dataObject, dataObject1):
print (single_date.strftime("%Y-%m-%d"))

改成这样:

mylist = []
#range
for single_date in daterange(dataObject, dataObject1):
mylist.append(single_date.strftime("%Y-%m-%d"))

关于Python:如何在汽车列表中存储日期范围的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50469887/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com