python时间戳转换
2015-12-09 00:00
393 查看
# -*- coding: utf-8 -*- import time,urllib2,re def timestamp_datetime(value): format = '%Y-%m-%d %H:%M:%S' # value为传入的值为时间戳(整形),如:1449641001 value = time.localtime(value) # 经过strftime函数转换为正常日期格式。 dt = time.strftime(format, value) return dt def datetime_timestamp(dt): # 一般都需要将字符串转化为时间数组 time.strptime(dt, '%Y-%m-%d %H:%M:%S') #将"2015-11-28 06:53:40"转化为时间戳 val = time.mktime(time.strptime(dt, '%Y-%m-%d %H:%M:%S')) return val if __name__ == '__main__': # 获取百度时间戳 req = urllib2.Request( url='http://open.baidu.com/special/time/') res = urllib2.urlopen(req,timeout=3).read() # 正则匹配 r = re.compile(r"baidu_time\((\d{10})") ts = re.findall(r, res)[0] value=float(ts) dt = timestamp_datetime(value) td = datetime_timestamp(dt) print u'当前时间: ',dt print u'当前时间戳:',td
>>> 当前时间: 2015-12-09 14:11:36 当前时间戳: 1449641496 >>>
相关文章推荐
- Python动态类型的学习---引用的理解
- Python3写爬虫(四)多线程实现数据爬取
- 垃圾邮件过滤器 python简单实现
- 下载并遍历 names.txt 文件,输出长度最长的回文人名。
- install and upgrade scrapy
- Scrapy的架构介绍
- Centos6 编译安装Python
- 使用Python生成Excel格式的图片
- 让Python文件也可以当bat文件运行
- [Python]推算数独
- Python中zip()函数用法举例
- Python中map()函数浅析
- Python将excel导入到mysql中
- Python在CAM软件Genesis2000中的应用
- 使用Shiboken为C++和Qt库创建Python绑定
- FREEBASIC 编译可被python调用的dll函数示例
- Python 七步捉虫法