转换日期格式python - 异常日期格式 - 提取%Y%M%D [英] Converting date formats python - Unusual date formats - Extract %Y%M%D
问题描述
我有一个大型数据集,其中包含以下格式的各种日期信息:
I have a large data set with a variety of Date information in the following formats:
- 自1900年1月1日起 - :41213 - 我相信这些都是从Excel http://www.kirix。 com / stratablog / jd-edwards-date-conversions-cyyddd
- YYDayofyear - ex 2012265
我熟悉python的时间模块,strptime()方法和strftime()方法。但是,如果有一个python模块可以用来转换这些不寻常的日期格式,我不知道这些日期格式是如何调用的。
I am familiar with python's time module, strptime() method, and strftime () method. However, I am not sure what these date formats above are called on if there is a python module I can use to convert these unusual date formats.
任何想法如何获取没有编写自己的计算器,这些不寻常的日期格式的%Y%M%D格式?
Any idea how to get the %Y%M%D format from these unusual date formats without writing my own calculator?
谢谢。
推荐答案
您可以尝试以下内容:
In [1]: import datetime
In [2]: s = '2012265'
In [3]: datetime.datetime.strptime(s, '%Y%j')
Out[3]: datetime.datetime(2012, 9, 21, 0, 0)
In [4]: d = '41213'
In [5]: datetime.date(1900, 1, 1) + datetime.timedelta(int(d))
Out[5]: datetime.date(2012, 11, 2)
第一个是棘手的一个,但它使用%j
参数来解释一天在您提供的年份(四位数年份之后,以%Y
为代表)。第二个只是从1900年1月1日起的天数。
The first one is the trickier one, but it uses the %j
parameter to interpret the day of the year you provide (after a four-digit year, represented by %Y
). The second one is simply the number of days since January 1, 1900.
这是一般的转换 - 不确定你的输入格式,但希望这可以调整适合它。
This is the general conversion - not sure of your input format but hopefully this can be tweaked to suit it.
这篇关于转换日期格式python - 异常日期格式 - 提取%Y%M%D的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!