python datetime strptime 通配符

发布于 2024-08-01 16:03:41 字数 267 浏览 4 评论 0原文

我想将这样的日期解析为日期时间对象:

  • 2008 年 12 月 12 日
  • 2009 年 1 月 1 日

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

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

但由于日期数字后缀(“st”),因此第二个日期将失败。 那么,strptime 中是否存在未记录的通配符? 或者更好的方法?

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

  • December 12th, 2008
  • January 1st, 2009

The following will work for the first date:

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

人疚 2024-08-08 16:03:41

尝试使用 dateutil.parser 模块。

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

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

Try using the dateutil.parser module.

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

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

镜花水月 2024-08-08 16:03:41

您需要 Gustavo Niemeyer 的 python_dateutil ——安装后,

>>> from dateutil import parser
>>> parser.parse('December 12th, 2008')
datetime.datetime(2008, 12, 12, 0, 0)
>>> parser.parse('January 1st, 2009')
datetime.datetime(2009, 1, 1, 0, 0)
>>> 

You need Gustavo Niemeyer's python_dateutil -- once it's installed,

>>> from dateutil import parser
>>> parser.parse('December 12th, 2008')
datetime.datetime(2008, 12, 12, 0, 0)
>>> parser.parse('January 1st, 2009')
datetime.datetime(2009, 1, 1, 0, 0)
>>> 
冬天旳寂寞 2024-08-08 16:03:41

strptime 很棘手,因为它依赖于底层 C 库来实现,因此平台之间的一些细节有所不同。 似乎没有办法匹配您需要的字符。 但你可以先清理数据:

# Remove ordinal suffixes from numbers.
date_in = re.sub(r"(st|nd|rd|th),", ",", date_in)
# Parse the pure date.
date = datetime.strptime(date_in, "%B %d, %Y")

strptime is tricky because it relies on the underlying C library for its implementation, so some details differ between platforms. There doesn't seem to be a way to match the characters you need to. But you could clean the data first:

# Remove ordinal suffixes from numbers.
date_in = re.sub(r"(st|nd|rd|th),", ",", date_in)
# Parse the pure date.
date = datetime.strptime(date_in, "%B %d, %Y")
风铃鹿 2024-08-08 16:03:41

如果你想使用任意通配符,你可以使用 datetime-glob,这是我们开发的一个模块从一致的日期/时间格式生成的文件列表中解析日期/时间。 来自模块的文档:

>>> import datetime_glob
>>> matcher = datetime_glob.Matcher(
                         pattern='/some/path/*%Y-%m-%dT%H-%M-%SZ.jpg')

>>> matcher.match(path='/some/path/some-text2016-07-03T21-22-23Z.jpg')
datetime_glob.Match(year = 2016, month = 7, day = 3, 
                    hour = 21, minute = 22, second = 23, microsecond = None)

>>> match.as_datetime()
datetime.datetime(2016, 7, 3, 21, 22, 23)

If you want to use arbitrary wildcards, you can use datetime-glob, a module we developed to parse date/times from a list of files generated by a consistent date/time formatting. From the module's documentation:

>>> import datetime_glob
>>> matcher = datetime_glob.Matcher(
                         pattern='/some/path/*%Y-%m-%dT%H-%M-%SZ.jpg')

>>> matcher.match(path='/some/path/some-text2016-07-03T21-22-23Z.jpg')
datetime_glob.Match(year = 2016, month = 7, day = 3, 
                    hour = 21, minute = 22, second = 23, microsecond = None)

>>> match.as_datetime()
datetime.datetime(2016, 7, 3, 21, 22, 23)
夜夜流光相皎洁 2024-08-08 16:03:41

对于像我一样只想要无需额外模块即可“工作”的人来说,这是一个快速而肮脏的解决方案。

string_list = ["th", "rd", "nd", "st"]
time = None
for str in string_list:
    if (time is not None):
        break
    try:
        match_string = '%B %d' + str +', %Y'
        time = datetime.strptime("December 12th, 2008", match_string)
    except Exception:
        pass

For anyone who, like me, just want something that "works" without an additional module, this is a quick and dirty solution.

string_list = ["th", "rd", "nd", "st"]
time = None
for str in string_list:
    if (time is not None):
        break
    try:
        match_string = '%B %d' + str +', %Y'
        time = datetime.strptime("December 12th, 2008", match_string)
    except Exception:
        pass
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文