arn*_*o_v 20 python python-2.7
我正在使用Paypal API,我以下列格式返回时间戳.它尝试使用strptime将其解析为datetime对象,但是我收到以下错误:
(Pdb) datetime.strptime('2012-03-01T10:00:00Z','%Y-%M-%dT%H:%M:%SZ')
*** error: redefinition of group name 'M' as group 5; was group 2
Run Code Online (Sandbox Code Playgroud)
此外,由于这种格式应该是一种非常标准的格式,因此没有可用的功能吗?
编辑:
好吧似乎是一个错字.首先%M应为%m
Mat*_*ock 40
来自dateutil的解析器是你的朋友.
你必须要安装dateutil,但你已经保存了包和日期转换代码包:
pip install python-dateutil
Run Code Online (Sandbox Code Playgroud)
你可以像这样使用它.
from dateutil import parser
ds = '2012-03-01T10:00:00Z' # or any date sting of differing formats.
date = parser.parse(ds)
Run Code Online (Sandbox Code Playgroud)
你会发现你可以使用这个解析器处理几乎任何日期字符串格式,你会得到一个很好的标准python日期
问题是你用了%M两次。使用%m月份:
>>> datetime.strptime('2012-03-01T10:00:00Z','%Y-%m-%dT%H:%M:%SZ')
datetime.datetime(2012, 3, 1, 10, 0)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
34159 次 |
| 最近记录: |