ValueError解析时间字符串 [英] ValueError parsing time string

查看:510
本文介绍了ValueError解析时间字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经写了这段代码将一个不寻常的时间转换成EPOCH:

I have written this code to convert a unusual time into EPOCH:

x = 'Mon Jul 25 19:04:30 GMT+01:00 2016'
print(datetime.strptime(x, '%a %b %d %H:%M:%S %Z%z %Y').strftime('%s'))

但是,它返回错误 ValueError:时间数据'Mon Jul 25 19:04:30 GMT + 01:00 2016不符合格式'%a%b%d%H:%M:%S%Z%z%Y'

该问题与时区有关。我做错了什么?

The problem is something to do with the timezone. What have I done wrong?

推荐答案

您的时区格式有一个额外的内部导致格式不匹配错误,您可以先从字符串中删除最后一个,然后解析:

Your timezone format has an extra : inside which causes the format mismatching error, you can remove the last : from the string firstly and then parse it:

import re
from datetime import datetime
x1 = re.sub(r":(?=[^:]+$)", "", x)   # remove the last semi colon

datetime.strptime(x1, '%a %b %d %H:%M:%S %Z%z %Y').strftime('%s')
# '1469487870'

这篇关于ValueError解析时间字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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