(转)Python格式化时间戳

好的: http://docs.python.org/library/datetime.html?highlight=datetime#datetime.tzinfo


来源:http://hi.baidu.com/mvp_xuan/blog/item/1a34cf0830581b9ed0581b74.html

 前因:需要在google的服务器上读取一个task list,结果发现这个list的time结果是时间戳的方式(相对于1970.1.1 00:00:00以秒计算的偏移量)。


原始处理数据如下:

    "createTime": "1319425189282",\n   "updateTime": "1319425189282",\n   "accessTime": "1319425189282",\n

代码如下:

——————————————————————————

import time

createValue = 1319425189282 #以毫秒为单位的时间,自1970年开始到现今
createValue = float(createValue)
createValue /= 1000 #除以1000的原因是gmtime这个方法只能转换秒级,而原始数据是毫秒

print time.gmtime(createValue)

print time.strftime('%Y-%m-%d %H:%M:%S', time.gmtime(createValue))

——————————————————————————

输出:

C:\Python26\python.exe C:/Users/mvp_xuan/Desktop/learnpython/test2.py
time.struct_time(tm_year=2011, tm_mon=10, tm_mday=24, tm_hour=2, tm_min=59, tm_sec=49, tm_wday=0, tm_yday=297, tm_isdst=0)
2011-10-24 02:59:49

Process finished with exit code 0

 

   哈哈,这下就可以处理这个毫秒级的数值了。





"slowRate":"%.4f" %(float(r[u"slowRate"]))
round(r[u"slowRate"],3) ,3表示保存几位小数。


records =[]
records.append({"channelId": "%s" %(r[u"channelId"]), "accessDate": "%s" %(r[u"accessDate"].strftime("%Y%m%d%H%M")),"slowSpeedCount": int(r[u"slowSpeedCount"]),"slowRate":"%.4f" %(float(r[u"slowRate"]))


可查看:
http://docs.python.org/library/functions.html?highlight=float#float


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值