[英]Extract Month From Julian day and year in python
I have essentially the opposite question as this question . 与这个问题我基本上有相反的问题 。 I have a string structured as
"%y%j"
such that January 1 2017 would be "17001"
and December 31 1995 would be "95365"
. 我有一个字符串,结构为
"%y%j"
,使得2017年1月1日为"17001"
,1995年12月31日为"95365"
。
I essentially need to know what month it is from any given input string. 我基本上需要从任何给定的输入字符串中知道它是几月。 I thought this would be relatively simple.
我认为这会相对简单。 Something like:
就像是:
input = "95365"
year = int(input[0:2])
day = int(input[2:5])
if day < 32:
month = 1
if day >= 32 and day < 50:
# etc...
what I failed to remember was leap years and how often they repeat. 我没有记住的是leap年,以及它们重复的频率。 Does anyone have an easy fix?
有人能轻松解决吗? Is there a library for this sort of thing?
是否有用于此类事情的图书馆?
Unless I am missing something, I think all you need is: 除非我缺少任何东西,否则我认为您只需要:
>>> import datetime
>>> s = '95365'
>>> datetime.datetime.strptime(s, '%y%j').month
12
>>> datetime.datetime.strptime(s, '%y%j').day
31
>>> datetime.datetime.strptime(s, '%y%j').year
1995
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.