'z' is a bad directive Python无法解析带时区的时间戳字符串
2017-11-10 19:26
232 查看
今天做服务器日志的数据处理,格式如
但是会报错
You can’t use %z in strptime because Python has no classes to represent timezones (you are supposed to implement your own, or better yet include some other libraries).
原来Python的时间解析不支持带时区的解析,那怎么办,索性不解析了,只保留当前的时刻就好,反正我不需要做全球的日志统计。
Python这里的坑,写篇短文记录一下。
"24/Oct/2017:17:07:05 +0800"。然后查找到python官网datetime格式解析文档,美滋滋写了代码实验一下。
import time time.strptime("24/Oct/2017:17:07:05 +0800", "%d/%b/%Y:%H:%M:%S %Z")
但是会报错
'z' is a bad directive in format '%d/%b/%Y:%H:%M:%S %Z'。Google一搜,发现stackoverflow早有问题z is a bad directive。回答中提到
You can’t use %z in strptime because Python has no classes to represent timezones (you are supposed to implement your own, or better yet include some other libraries).
原来Python的时间解析不支持带时区的解析,那怎么办,索性不解析了,只保留当前的时刻就好,反正我不需要做全球的日志统计。
import time time.strptime("24/Oct/2017:17:07:05 +0800".split()[0], "%d/%b/%Y:%H:%M:%S")
Python这里的坑,写篇短文记录一下。
相关文章推荐
- 将UTC(字符串包含TZ的时间)时间转换成本地时间 python
- c++中调用python脚本提示 error LNK2001: 无法解析的外部符号
- Python的时间:秒和字符串之间的转换
- Python的3种字符串连接的时间比较
- Python2基础用法(字符串,list,元组,字典解析)
- Python 获取时间字符串
- Python 输入时间字符串以分钟单位计算时间差
- Python的time(时间戳与时间字符串互相转化)
- Python中时间戳与时间字符串互相转化
- Python中时间戳与时间字符串互相转化
- Java实现字符串解析为日期时间的方法示例
- python中原始字符串解析
- spring文件无法解析占位符,按照字符串解析
- python cookbook第三版学习笔记四:文本以及字符串令牌解析
- IOS获取当前时区时间,并截取年月日输出字符串
- PHP 字符串加密函数(在指定时间内加密还原字符串,超时无法还原)
- python中时间戳、字符串之间转换详解
- PHP 输出json字符串无法解析 以前正常
- [转] 强大的python字符串解析