python datetime strptime通配符 [英] python datetime strptime wildcard

查看:531
本文介绍了python datetime strptime通配符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将这些日期解析为datetime对象:

I want to parse dates like these into a datetime object:


  • 2008年12月12日

  • 2009年1月1日

以下内容将适用于第一个日期:

The following will work for the first date:

datetime.strptime("December 12th, 2008", "%B %dth, %Y")

但由于后缀到日数('st'),因此第二个失败。那么,strptime中有没有文档的通配符?或者一个更好的方法?

but will fail for the second because of the suffix to the day number ('st'). So, is there an undocumented wildcard character in strptime? Or a better approach altogether?

推荐答案

尝试使用dateutil.parser模块。

Try using the dateutil.parser module.

import dateutil.parser
date1 = dateutil.parser.parse("December 12th, 2008")
date2 = dateutil.parser.parse("January 1st, 2009")

其他文档可以在这里找到: http://labix.org/python-dateutil

Additional documentation can be found here: http://labix.org/python-dateutil

这篇关于python datetime strptime通配符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆