在 Python 中解析非零填充时间戳
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25279993/
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
Parsing non-zero padded timestamps in Python
提问by Pythontology
I want to get datetimes from timestamps like the following :3/1/2014 9:55with datetime.strptime, or something equivalent.
我想从时间戳中获取日期时间,如下所示:3/1/2014 9:55withdatetime.strptime或等效的东西。
The month, day of month, and hour is not zero padded, but there doesn't seem to be a formatting directive listed herethat is able to parse this automatically.
月份,月中的一天,时不补零,但似乎没有要上市的格式化指令,在这里即能自动分析此。
What's the best approach to do so? Thanks!
这样做的最佳方法是什么?谢谢!
采纳答案by Jason S
strptimeis able to parse non-padded values. The fact that they are noted as being padded in the formatting codes table applies to strftime's output. So you can just use
strptime能够解析非填充值。它们在格式化代码表中被填充的事实适用于strftime的输出。所以你可以使用
datetime.strptime(datestr, "%m/%d/%Y %H:%M")
回答by gaw
strptimeisdo not require 0-padded values. See example below
strptime不需要 0 填充值。请参阅下面的示例
datetime.strptime("3/1/2014 9:55", "%m/%d/%Y %H:%M")
output: datetime.datetime(2014, 3, 1, 9, 55)
回答by xecgr
The non-pattern way is use dateutil.parsemodule, it lets to parse the common date formats, even if you don't know what it is using currently
Ex:
非模式方式是使用dateutil.parse模块,它可以解析常见的日期格式,即使您不知道它当前使用的是什么
Ex:
>>> import dateutil.parser
>>>
>>> utc_time = '2014-08-13T00:00:00'
>>> verbose_time = '13-Aug-2014'
>>> some_locale = '3/1/2014 9:55'
>>> dateutil.parser.parse(utc_time)
datetime.datetime(2014, 8, 13, 0, 0)
>>> dateutil.parser.parse(verbose_time)
datetime.datetime(2014, 8, 13, 0, 0)
>>> dateutil.parser.parse(some_locale)
datetime.datetime(2014, 3, 1, 9, 55)
回答by hwjp
Just in case this answer helps someone else -- I came here thinking I had a problem with zero padding, but it was actually to do with 12:00 vs 00:00 and the %Iformatter.
以防万一这个答案对其他人有帮助——我来到这里以为我在零填充方面遇到了问题,但这实际上与 12:00 vs 00:00 和%I格式化程序有关。
The %Iformatter is meant to match 12-hour-clock hours, optionally zero-padded. But depending on your data source, you might get data that says that midnight or midday is actually zero, eg:
该%I格式化器是指以匹配12小时的时钟小时,任选零填充。但是根据您的数据源,您可能会得到表明午夜或中午实际上为零的数据,例如:
>>> datetime.strptime('2015/01/01 0:12am', "%Y/%m/%d %I:%M%p")
ValueError: time data '2015/01/01 0:12am' does not match format '%Y/%m/%d %I:%M'
What strptimeactually wanted was a 12, not a zero:
什么strptime真正想要的是一个12,而不是一个零:
>>> datetime.strptime('2015/01/01 12:12am', "%Y/%m/%d %I:%M%p")
datetime.datetime(2015, 1, 1, 0, 12)
But we don't always control our data sources! My solution for this edge case was to catch the exception, try parsing it with a %H, with a quick check that we are in the edge case we think we are in.
但我们并不总是控制我们的数据源!我对这种边缘情况的解决方案是捕获异常,尝试用 a 解析它%H,并快速检查我们是否处于我们认为处于的边缘情况。
def get_datetime(string):
try:
timestamp = datetime.strptime(string, "%m/%d/%Y %I:%M%p")
except ValueError:
# someone used zero for midnight?
timestamp = datetime.strptime(string, "%m/%d/%Y %H:%M%p")
assert string.lower().endswith('am')
assert timestamp.hour == 0
return timestamp
回答by u5563257
You can see the strftime document here,but in fact they aren't all working well in all platforms,for instance,%-d,%-mdon't work on win7 by python 2.7,so you can accomplish like this
你可以在这里看到 strftime 文档,但实际上它们并不是在所有平台上都运行良好,例如,%-d,%-mpython 2.7 不能在 win7 上运行,所以你可以这样完成
>>> date_str = '{d.year}-{d.month}-{d.day}'.format(d=datetime.datetime.now())
>>> print(date_str)
2016-5-23

