【问题标题】:Increment date by day and input in new string按天递增日期并输入新字符串
【发布时间】:2019-07-25 21:45:05
【问题描述】:

不确定如何按天递增日期并将新日期插入现有字符串。

即:"...dates": '2017-01-01'...

增加到 ..."dates": '2017-01-02'... 并输入到elasticsearch中"dates"字段中的现有字符串

请帮忙,我在 Python 中找不到类似的东西,只有在 Java 中。

【问题讨论】:

  • 字符串在 Python 中没有“字段”。

标签: python string loops date


【解决方案1】:
In [23]: import datetime as dt                                                                                                                                                                                                                                                                                                

In [24]: day1 = dt.datetime.strptime("2017-01-01", "%Y-%m-%d")                                                                                                                                                                                                                                                                

In [25]: for i in range(10): 
    ...:     day = day1 + dt.timedelta(days=i) 
    ...:     print("dates:", day.strftime("%Y-%m-%d")) 
    ...:                                                                                                                                                                                                                                                                                                                      
dates: 2017-01-01
dates: 2017-01-02
dates: 2017-01-03
dates: 2017-01-04
dates: 2017-01-05
dates: 2017-01-06
dates: 2017-01-07
dates: 2017-01-08
dates: 2017-01-09
dates: 2017-01-10

【讨论】:

  • 有没有办法访问要打印的每一行日期?因为当我执行 x = day.strftime('%Y-%m-%d') 然后打印 x 时,只输出最后一个日期,而不是所有日期
  • @aesthetics:我感觉你是在循环之外做的。将 x = ... 粘贴在 for 循环中,您应该没问题...或将所有内容附加到列表中
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-01-05
  • 2015-08-26
  • 2014-09-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多