如何从Python中包含'2nd'的字符串中获取日期时间?

时间:2020-12-28 13:12:41

I've got a couple strings from which I want to get the datetime. They are formatted like this:

我有几个字符串,我希望得到日​​期时间。它们的格式如下:

Thu 2nd May 2013 19:00

I know almost how I can convert this to a datetime, except for that I'm having trouble with the "2nd". I now have the following

我几乎知道如何将其转换为日期时间,除非我遇到“第二”问题。我现在有以下内容

>>> datetime.strptime('Thu 02 May 2013 19:00', '%a %d %B %Y %H:%M')
datetime.datetime(2013, 5, 2, 19, 0)

which works fine with a zero padded number for the day of the month, but when I try the 2nd, it gives a ValueError:

这个月的日期工作正常,零填充数字,但是当我尝试第二个时,它会产生一个ValueError:

>>> datetime.strptime('Thu 2nd May 2013 19:00', '%a %d %B %Y %H:%M')
Traceback (most recent call last):
  File "<input>", line 1, in <module>
    (data_string, format))
ValueError: time data 'Thu 2nd May 2013 19:00' does not match format '%a %d %B %Y %H:%M'

In the list of datetime directives I can't find anything relating to ordered values (1st, 2nd, 3rd etc) for dates. Does anybody know how I can get this to work? All tips are welcome!

在datetime指令列表中,我找不到与日期的有序值(第1,第2,第3等)相关的任何内容。有谁知道我怎么能让这个工作?欢迎所有提示!

4 个解决方案

#1


7  

Consider using dateutil.parser.parse.

考虑使用dateutil.parser.parse。

It's a third party library that has a powerful parser which can handle these kinds of things.

这是一个第三方库,它有一个强大的解析器,可以处理这些事情。

from dateutil.parser import parse

s = 'Thu 2nd May 2013 19:00'

d = parse(s)
print(d, type(d))
# 2013-05-02 19:00:00 <class 'datetime.datetime'>

A brief caveat (doesn't really occur in your case): if dateutil can't find an aspect of your date in the string (say you leave out the month) then it will default to the default argument. This defaults to the current date with the time 00:00:00. You can obviously over-write this if necessary with a different datetime object.

一个简短的警告(在你的情况下并没有真正发生):如果dateutil在字符串中找不到你日期的一个方面(比如你省略了月份),那么它将默认为默认参数。默认为当前日期,时间为00:00:00。显然,如果需要,可以使用不同的datetime对象覆盖它。

The easiest way to install dateutil is probably using pip with the command pip install python-dateutil.

安装dateutil的最简单方法可能是使用命令pip install python-dateutil使用pip。

#2


4  

You can preparse the original string to adjust the day to be suitable for your strptime, eg:

您可以预先填充原始字符串以调整适合您的strptime的日期,例如:

from datetime import datetime
import re

s = 'Thu 2nd May 2013 19:00'
amended = re.sub('\d+(st|nd|rd|th)', lambda m: m.group()[:-2].zfill(2), s)
# Thu 02 May 2013 19:00
dt = datetime.strptime(amended, '%a %d %B %Y %H:%M')
# 2013-05-02 19:00:00

#3


2  

It's straightforward to remove the suffix from the date without using regular expressions or an external library.

在不使用正则表达式或外部库的情况下从日期中删除后缀非常简单。

def remove_date_suffix(s):
    parts = s.split()
    parts[1] = parts[1].strip("stndrh") # remove 'st', 'nd', 'rd', ...
    return " ".join(parts)

Then it's as simple as using strptime as you'd expect:

然后它就像你期望的那样使用strptime一样简单:

>>> s = "Thu 2nd May 2013 19:00"
>>> remove_date_suffix(s)
'Thu 2 May 2013 19:00'
>>> datetime.strptime(remove_date_suffix(s), '%a %d %B %Y %H:%M')
datetime.datetime(2013, 5, 2, 19, 0)

#4


0  

import re
from datetime import datetime
def proc_date(x):
    return re.sub(r"\b([0123]?[0-9])(st|th|nd|rd)\b",r"\1",x)

>>> x='Thu 2nd May 2013 19:00'
>>> proc_date(x)
'Thu 2 May 2013 19:00'
>>> datetime.strptime(proc_date(x), '%a %d %B %Y %H:%M')
datetime.datetime(2013, 5, 2, 19, 0)

#1


7  

Consider using dateutil.parser.parse.

考虑使用dateutil.parser.parse。

It's a third party library that has a powerful parser which can handle these kinds of things.

这是一个第三方库,它有一个强大的解析器,可以处理这些事情。

from dateutil.parser import parse

s = 'Thu 2nd May 2013 19:00'

d = parse(s)
print(d, type(d))
# 2013-05-02 19:00:00 <class 'datetime.datetime'>

A brief caveat (doesn't really occur in your case): if dateutil can't find an aspect of your date in the string (say you leave out the month) then it will default to the default argument. This defaults to the current date with the time 00:00:00. You can obviously over-write this if necessary with a different datetime object.

一个简短的警告(在你的情况下并没有真正发生):如果dateutil在字符串中找不到你日期的一个方面(比如你省略了月份),那么它将默认为默认参数。默认为当前日期,时间为00:00:00。显然,如果需要,可以使用不同的datetime对象覆盖它。

The easiest way to install dateutil is probably using pip with the command pip install python-dateutil.

安装dateutil的最简单方法可能是使用命令pip install python-dateutil使用pip。

#2


4  

You can preparse the original string to adjust the day to be suitable for your strptime, eg:

您可以预先填充原始字符串以调整适合您的strptime的日期,例如:

from datetime import datetime
import re

s = 'Thu 2nd May 2013 19:00'
amended = re.sub('\d+(st|nd|rd|th)', lambda m: m.group()[:-2].zfill(2), s)
# Thu 02 May 2013 19:00
dt = datetime.strptime(amended, '%a %d %B %Y %H:%M')
# 2013-05-02 19:00:00

#3


2  

It's straightforward to remove the suffix from the date without using regular expressions or an external library.

在不使用正则表达式或外部库的情况下从日期中删除后缀非常简单。

def remove_date_suffix(s):
    parts = s.split()
    parts[1] = parts[1].strip("stndrh") # remove 'st', 'nd', 'rd', ...
    return " ".join(parts)

Then it's as simple as using strptime as you'd expect:

然后它就像你期望的那样使用strptime一样简单:

>>> s = "Thu 2nd May 2013 19:00"
>>> remove_date_suffix(s)
'Thu 2 May 2013 19:00'
>>> datetime.strptime(remove_date_suffix(s), '%a %d %B %Y %H:%M')
datetime.datetime(2013, 5, 2, 19, 0)

#4


0  

import re
from datetime import datetime
def proc_date(x):
    return re.sub(r"\b([0123]?[0-9])(st|th|nd|rd)\b",r"\1",x)

>>> x='Thu 2nd May 2013 19:00'
>>> proc_date(x)
'Thu 2 May 2013 19:00'
>>> datetime.strptime(proc_date(x), '%a %d %B %Y %H:%M')
datetime.datetime(2013, 5, 2, 19, 0)