您的位置:首页 > 编程语言 > Python开发

(转)Python格式化时间戳

2012-04-28 15:39 309 查看
好的: 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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: