无法解析 Python 中的日期 [英] Cannot parse the date in Python

查看:80
本文介绍了无法解析 Python 中的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要解析日期和时间.这是我所拥有的:

I need to parse date and time. Here is what I've got:

import time
a = time.strptime('Apr 28 2013 23:01', "%b %d %y %H:%M")
print a 

但它给了我

Traceback (most recent call last):
  File "/home/aaa/Documents/python_test.py", line 17, in <module>
    a = time.strptime('Apr 28 2013 23:01', "%b %d %y %H:%M")
  File "/usr/lib/python2.7/_strptime.py", line 467, in _strptime_time
    return _strptime(data_string, format)[0]
  File "/usr/lib/python2.7/_strptime.py", line 325, in _strptime
    (data_string, format))
ValueError: time data 'Apr 28 2013 23:01' does not match format '%b %d %y %H:%M'

我做错了什么?

推荐答案

%y 应该是 %Y 表示 4 位数字的年份...

%y should be %Y for a 4 digit year...

来自文档:

%y  Year without century as a decimal number [00,99].    
%Y  Year with century as a decimal number.

这篇关于无法解析 Python 中的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆