我希望获得多年的第一天和最后一天,即每个月中python的日期范围。
我尝试如下:
import datetime, numpy as np
dates = np.arange(np.datetime64('2014-01-01'), np.datetime64('2015-01-01'))
dates = [datetime.datetime.strptime(str(x),"%Y-%m-%d") for x in dates]
DayOfYears = [datetime.datetime.strptime(str(x),"%j") for x in dates]
Months = [datetime.datetime.strptime(str(x),"%B") for x in dates]
我的代码无法获取DayOfYears和Months。它正在提供 ValueError:未转换的数据仍然存在:4-01-01 00:00:00 < / p>
最后,我必须以这种形式打印出结果:
January 01 31
February 32 60
May 122 152
December 335 365
这样做的好方法怎么样?伙计们?
编辑:
input_dates = ['2014-01-01', '2014-01-02', '2014-02-03', '2014-02-04', '2014-05-05', '2014-05-06', '2014-05-07', '2014-05-08']
output_answer:
January 1 31
February 32 60
May 122 152.
答案 0 :(得分:4)
import calendar
from datetime import date
from dateutil.relativedelta import relativedelta
start = date(2012,1,1)
end = date(2015,1,1)
while start < end:
first_delta = (start - date(start.year, 1, 1)).days + 1
last_delta = (start + relativedelta(days=-1, months=1) - date(start.year, 1, 1)).days + 1
print calendar.month_name[start.month], '%02d' % first_delta, last_delta
start += relativedelta(months=1)
输出:
January 01 31
February 32 60
March 61 91
...
答案 1 :(得分:1)
import calendar
start = datetime.datetime(2012,01,01)
end = datetime.datetime(2015,01,01)
e = 0
while start != end:
months = calendar.monthrange(start.year,start.month)
e += months[1]
print("{} {} {}".format(start.strftime("%B"),e-months[1]+1, e))
start += datetime.timedelta(days=months[1])
January 1 31
February 32 60
March 61 91
April 92 121
May 122 152
June 153 182
......
你有日期时间对象,所以使用strftime,你试图解析像2014-01-01
这样不匹配%j
的字符串,这就是你收到错误的原因:
dayOfYears = [x.strftime("%j") for x in dates]
几个月来一样:
Months = [x.strftime("%B") for x in dates]
现在几天和几个月看起来像:
['001', '002', '003', '004', '005', '006', '007', '008', '009', '010', '011', '012', '013', '014', '015', '016', '017', '018', '019', '020', '021', '022', '023', '024', '025', '026', '027', '028', '029', '030', '031', '032', '033', '034', '035', '036', '037', '038', '039', '040', '041', '042', '043', '044', '045', '046', '047', '048', '049', '050', '051', '052', '053', '054', '055', '056', '057', '058', '059', '060', '061', '062', '063', '064', '065', '066', '067', '068', '069', '070', '071', '072', '073', '074', '075', '076', '077', '078', '079', '080', '081', '082', '083', '084', '085', '086', '087', '088', '089', '090', '091', '092', '093', '094', '095', '096', '097', '098', '099', '100', '101', '102', '103', '104', '105', '106', '107', '108', '109', '110', '111', '112', '113', '114', '115', '116', '117', '118', '119', '120', '121', '122', '123', '124', '125', '126', '127', '128', '129', '130', '131', '132', '133', '134', '135', '136', '137', '138', '139', '140', '141', '142', '143', '144', '145', '146', '147', '148', '149', '150', '151', '152', '153', '154', '155', '156', '157', '158', '159', '160', '161', '162', '163', '164', '165', '166', '167', '168', '169', '170', '171', '172', '173', '174', '175', '176', '177', '178', '179', '180', '181', '182', '183', '184', '185', '186', '187', '188', '189', '190', '191', '192', '193', '194', '195', '196', '197', '198', '199', '200', '201', '202', '203', '204', '205', '206', '207', '208', '209', '210', '211', '212', '213', '214', '215', '216', '217', '218', '219', '220', '221', '222', '223', '224', '225', '226', '227', '228', '229', '230', '231', '232', '233', '234', '235', '236', '237', '238', '239', '240', '241', '242', '243', '244', '245', '246', '247', '248', '249', '250', '251', '252', '253', '254', '255', '256', '257', '258', '259', '260', '261', '262', '263', '264', '265', '266', '267', '268', '269', '270', '271', '272', '273', '274', '275', '276', '277', '278', '279', '280', '281', '282', '283', '284', '285', '286', '287', '288', '289', '290', '291', '292', '293', '294', '295', '296', '297', '298', '299', '300', '301', '302', '303', '304', '305', '306', '307', '308', '309', '310', '311', '312', '313', '314', '315', '316', '317', '318', '319', '320', '321', '322', '323', '324', '325', '326', '327', '328', '329', '330', '331', '332', '333', '334', '335', '336', '337', '338', '339', '340', '341', '342', '343', '344', '345', '346', '347', '348', '349', '350', '351', '352', '353', '354', '355', '356', '357', '358', '359', '360', '361', '362', '363', '364', '365']
['January', 'January', 'January', 'January', 'January', 'January', 'January', 'January', 'January', 'January', 'January', 'January', 'January', 'January', 'January', 'January', 'January', 'January', 'January', 'January', 'January', 'January', 'January', 'January', 'January', 'January', 'January', 'January', 'January', 'January', 'January', 'February', 'February', 'February', 'February', 'February', 'February', 'February', 'February', 'February', 'February', 'February', 'February', 'February', 'February', 'February', 'February', 'February', 'February', 'February', 'February', 'February', 'February', 'February', 'February', 'February', 'February', 'February', 'February', 'March', 'March', 'March', 'March', 'March', 'March', 'March', 'March', 'March', 'March', 'March', 'March', 'March', 'March', 'March', 'March', 'March', 'March', 'March', 'March', 'March', 'March', 'March', 'March', 'March', 'March', 'March', 'March', 'March', 'March', 'March', 'April', 'April', 'April', 'April', 'April', 'April', 'April', 'April', 'April', 'April', 'April', 'April', 'April', 'April', 'April', 'April', 'April', 'April', 'April', 'April', 'April', 'April', 'April', 'April', 'April', 'April', 'April', 'April', 'April', 'April', 'May', 'May', 'May', 'May', 'May', 'May', 'May', 'May', 'May', 'May', 'May', 'May', 'May', 'May', 'May', 'May', 'May', 'May', 'May', 'May', 'May', 'May', 'May', 'May', 'May', 'May', 'May', 'May', 'May', 'May', 'May', 'June', 'June', 'June', 'June', 'June', 'June', 'June', 'June', 'June', 'June', 'June', 'June', 'June', 'June', 'June', 'June', 'June', 'June', 'June', 'June', 'June', 'June', 'June', 'June', 'June', 'June', 'June', 'June', 'June', 'June', 'July', 'July', 'July', 'July', 'July', 'July', 'July', 'July', 'July', 'July', 'July', 'July', 'July', 'July', 'July', 'July', 'July', 'July', 'July', 'July', 'July', 'July', 'July', 'July', 'July', 'July', 'July', 'July', 'July', 'July', 'July', 'August', 'August', 'August', 'August', 'August', 'August', 'August', 'August', 'August', 'August', 'August', 'August', 'August', 'August', 'August', 'August', 'August', 'August', 'August', 'August', 'August', 'August', 'August', 'August', 'August', 'August', 'August', 'August', 'August', 'August', 'August', 'September', 'September', 'September', 'September', 'September', 'September', 'September', 'September', 'September', 'September', 'September', 'September', 'September', 'September', 'September', 'September', 'September', 'September', 'September', 'September', 'September', 'September', 'September', 'September', 'September', 'September', 'September', 'September', 'September', 'September', 'October', 'October', 'October', 'October', 'October', 'October', 'October', 'October', 'October', 'October', 'October', 'October', 'October', 'October', 'October', 'October', 'October', 'October', 'October', 'October', 'October', 'October', 'October', 'October', 'October', 'October', 'October', 'October', 'October', 'October', 'October', 'November', 'November', 'November', 'November', 'November', 'November', 'November', 'November', 'November', 'November', 'November', 'November', 'November', 'November', 'November', 'November', 'November', 'November', 'November', 'November', 'November', 'November', 'November', 'November', 'November', 'November', 'November', 'November', 'November', 'November', 'December', 'December', 'December', 'December', 'December', 'December', 'December', 'December', 'December', 'December', 'December', 'December', 'December', 'December', 'December', 'December', 'December', 'December', 'December', 'December', 'December', 'December', 'December', 'December', 'December', 'December', 'December', 'December', 'December', 'December', 'December']
对于您的编辑,我会使用以下内容:
import calendar
input_dates = ['2014-01-01', '2014-01-02', '2014-02-03', '2014-02-04', '2014-05-05', '2014-05-06', '2014-05-07', '2014-05-08']
dates = [datetime.datetime.strptime(str(x),"%Y-%m-%d") for x in input_dates]
seen = set()
for start in dates:
if not (start.year, start.month) in seen:
mon = datetime.datetime(start.year, start.month, 1)
year_day = mon.strftime("%j")
days = calendar.monthrange(mon.year,mon.month)[1]
print("{} {} {}".format(start.strftime("%B"), mon.strftime("%j"), days+int(year_day)))
seen.add((start.year,start.month))
January 001 32
February 032 60
May 121 152