从Python对象中提取日期

时间:2017-05-31 09:41:18

标签: python datetime

我有一个变量,它是一个Python对象,包含有关记录信息的日期和时间的信息。它采用以下格式

Date_time_Created
2012-06-29 11:23:44.882
2012-07-27 14:53:46.909
2014-05-01 16:12:32.005
2014-10-27 18:25:57.403
2014-10-29 10:58:46.013
2014-11-06 22:24:24.872
2014-11-06 22:26:13.218
2015-02-17 04:48:35.229
2015-04-14 07:54:43.969
2015-04-14 07:58:42.896
2015-04-14 08:00:35.552
2015-04-14 08:10:11.627
2015-04-14 08:14:37.842
2015-04-14 08:18:47.501
2015-04-14 08:27:21.895
2015-04-14 08:30:21.376
2015-04-14 08:33:42.146
2015-04-14 08:38:06.271
2015-04-14 08:44:36.056
2015-04-14 08:49:40.434
2015-04-14 08:56:38.785
2015-04-14 08:59:12.542
2015-04-14 09:01:19.538

我希望能够从中提取日期(虽然知道如何做时间也很有用!)。

我尝试过以下'应该'使用字符串

match = re.search(r'\d{4}-\d{2}-\d{2}', text)
date = datetime.strptime(match.group(), '%Y-%m-%d').date()

但是我正在使用的数据的数据类型是一个Python对象。

有什么想法吗?

提前致谢!

3 个答案:

答案 0 :(得分:1)

它不仅仅是一个日期时间对象吗?

如果是这样,你可以像

那样
dataobject.date # for year, month, day
dataobject.time # for hour, min, sec, mill

https://docs.python.org/2/library/datetime.html

如果您使用的是pandas库,可以尝试这样的方法来解析系列:

date = pd.to_datetime(data_object, format='%Y-%m-%d').dt.date

http://pandas.pydata.org/pandas-docs/version/0.20/generated/pandas.to_datetime.html

答案 1 :(得分:1)

假设您的数据位于stop文件中:

your.csv

输出将是:

from datetime import datetime
with open('your.csv') as date_file:
    next(date_file)    
    for line in date_file:
        date_object = datetime.strptime(line.strip(),'%Y-%m-%d %H:%M:%S.%f')
        print("date:%s, time:%s"%(date_object.date(),date_object.time()))

<强>更新 熊猫版:

date:2012-06-29, time:11:23:44.882000
date:2012-07-27, time:14:53:46.909000
date:2014-05-01, time:16:12:32.005000
date:2014-10-27, time:18:25:57.403000
date:2014-10-29, time:10:58:46.013000
date:2014-11-06, time:22:24:24.872000
date:2014-11-06, time:22:26:13.218000
date:2015-02-17, time:04:48:35.229000
date:2015-04-14, time:07:54:43.969000
date:2015-04-14, time:07:58:42.896000
date:2015-04-14, time:08:00:35.552000
date:2015-04-14, time:08:10:11.627000
date:2015-04-14, time:08:14:37.842000
date:2015-04-14, time:08:18:47.501000
date:2015-04-14, time:08:27:21.895000
date:2015-04-14, time:08:30:21.376000
date:2015-04-14, time:08:33:42.146000
date:2015-04-14, time:08:38:06.271000
date:2015-04-14, time:08:44:36.056000
date:2015-04-14, time:08:49:40.434000
date:2015-04-14, time:08:56:38.785000
date:2015-04-14, time:08:59:12.542000
date:2015-04-14, time:09:01:19.538000

答案 2 :(得分:0)

方法1: 您可以使用 dateutil https://dateutil.readthedocs.io/en/stable/

import dateutil.parser
yourdate = dateutil.parser.parse(datestring)

如果您想手动执行此操作,请稍微修改您提供的代码: import re,datetime 方法2:

 text ="Date_time_Created 2012-06-29 11:23:44.882 2012-07-27 14:53:46.909 2014-05-01 16:12:32.005 2014-10-27 18:25:57.403 2014-10-29 10:58:46.013 2015-04-14 08:56:38.785 2015-04-14 08:59:12.542 2015-04-14 09:01:19.538"

match = re.findall(r'\d{4}-\d{2}-\d{2}', text)
for i in match:
  date = datetime.datetime.strptime(i, '%Y-%m-%d').date()
  print(date)

如果用re.finall替换了re.search,那么我得到一个包含所有日期的列表,然后我解析它以将它们转换为日期。你可以按照自己的意愿处理它。