如何将字符串转换为数组列表

时间:2016-11-28 05:18:07

标签: python python-3.x

我已编写代码来生成给定月份的所有日期:

nb_days = monthrange(2016, 2)[1]
dateDict = [datetime.date(year, month, day) for day in range(1, nb_days+1)]
outputItems = []
for dd in dateDict:
    outputItems.append(dd.strftime('%Y-%m-%d'))

我的输出是这样的:

['2016-02-01', '2016-02-02', '2016-02-03', '2016-02-04', '2016-02-05', '2016-02-06', '2016-02-07', '2016-02-08', '2016-02-09', '2016-02-10', '2016-02-11', '2016-02-12', '2016-02-13', '2016-02-14', '2016-02-15', '2016-02-16', '2016-02-17', '2016-02-18', '2016-02-19', '2016-02-20', '2016-02-21', '2016-02-22', '2016-02-23', '2016-02-24', '2016-02-25', '2016-02-26', '2016-02-27', '2016-02-28', '2016-02-29']

我需要获得类似的结果:

[('2016-02-01', '0', '0', '0'), ('2016-02-02', '0', '0', '0'), ('2016-02-03', '0', '0', '0'), ('2016-02-04', '0', '0', '0'), .....]

1 个答案:

答案 0 :(得分:1)

试试这个outputItems.append( (dd.strftime('%Y-%m-%d'), '0', '0', '0') )

nb_days = monthrange(2016, 2)[1]
dateDict = [datetime.date(year, month, day) for day in range(1, nb_days+1)]
outputItems = []
for dd in dateDict:
    outputItems.append( (dd.strftime('%Y-%m-%d'), '0', '0', '0') )