
[英]Python Strptime not returning a date, but returning entire text file
[英]Python strptime with variable text
我有一个日期列表作为字符串。 它看起来像这样:
[
"January 29-30 Meeting - 2013",
"March 19-20 Meeting - 2013",
"April/May 30-1 Meeting - 2013",
"June 18-19 Meeting - 2013",
"July 30-31 Meeting - 2013",
"September 17-18 Meeting - 2013",
"October 29-30 Meeting - 2013",
"December 17-18 Meeting - 2013"
]
我需要将这些日期解析为datetime
格式。
datetime.strptime("January 29-30 Meeting - 2013", "%B %d-[something] - %Y")
datetime.strptime("January 29-30 Meeting - 2013", "%B [something]-%d [something] - %Y")
有没有什么方法可以告诉strptime,在格式说明符中,忽略[something]
的文本,因为它可以变量? 是否有可变文本的格式说明符?
strptime
没有通配符指令。 您可以在此处查看指令列表https://docs.python.org/3/library/time.html#time.strftime
解决问题的一种明智方法是将正则表达式与strptime
结合起来。 即使用正则表达式过滤掉文本并将剩余的受限文本放入strptime
,或者将匹配的组直接传递到datetime
。
import re
from datetime import datetime
ss = [
"January 29-30 Meeting - 2013",
"March 19-20 Meeting - 2013",
"April/May 30-1 Meeting - 2013",
"June 18-19 Meeting - 2013",
"July 30-31 Meeting - 2013",
"September 17-18 Meeting - 2013",
"October 29-30 Meeting - 2013",
"December 17-18 Meeting - 2013"
]
FORMAT = '%B %d %Y'
for s in ss:
match = re.search(r"(\w+)\s(\d+)-(\d+)\s.*\s(\d{4})", s)
if match:
dt1 = datetime.strptime(f'{match.group(1)} {match.group(2)} {match.group(4)}', FORMAT)
dt2 = datetime.strptime(f'{match.group(1)} {match.group(3)} {match.group(4)}', FORMAT)
print (dt1, dt2)
请注意,你也有April/May 30-1
并发症,我没有解决这个问题,因为你没有问这个问题。
作为奖励虽然:
for s in ss:
match = re.search(r"((\w+)/)?(\w+)\s(\d+)-(\d+)\s.*\s(\d{4})", s)
if match:
dt1 = datetime.strptime(
f'{match.group(2) if match.group(2) else match.group(3)} {match.group(4)} {match.group(6)}', FORMAT)
dt2 = datetime.strptime(
f'{match.group(3)} {match.group(5)} {match.group(6)}', FORMAT)
print (dt1, dt2)
另外,请注意有趣的,如果@blhsing提供的有点hacky解决方案,涉及_strptime.TimeRE
。 我不建议做那样的事情,但有趣的是你知道你实际上可以改变strptime
本身的行为。
您可以使用延迟匹配任何字符序列的附加指令覆盖_strptime.TimeRE
对象:
from datetime import datetime
import _strptime
TimeRE = _strptime.TimeRE()
TimeRE.update({'x': '.*?'})
_strptime._TimeRE_cache = TimeRE
print(datetime.strptime("January 29-30 Meeting - 2013", "%B %d-%x - %Y"))
这输出:
2013-01-29 00:00:00
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.