python datetime strptime 通配符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1258199/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
python datetime strptime wildcard
提问by hoju
I want to parse dates like these into a datetime object:
我想将这些日期解析为 datetime 对象:
- December 12th, 2008
- January 1st, 2009
- 2008 年 12 月 12 日
- 2009 年 1 月 1 日
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?
但由于天数('st')的后缀,第二次会失败。那么,strptime 中是否存在未公开的通配符?或者完全更好的方法?
采纳答案by Greg
Try using the dateutil.parser module.
尝试使用 dateutil.parser 模块。
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
可以在此处找到其他文档:http: //labix.org/python-dateutil
回答by Alex Martelli
You need Gustavo Niemeyer's python_dateutil-- once it's installed,
您需要 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)
>>>
回答by Ned Batchelder
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:
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")
回答by ava
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
回答by marko.ristin
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:
如果您想使用任意通配符,您可以使用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)