Python 从使用json.loads检索的历元时间中删除ms
我有一个管道分隔文件,其中一个字段包含JSON格式的一些信息:Python 从使用json.loads检索的历元时间中删除ms,python,python-2.x,Python,Python 2.x,我有一个管道分隔文件,其中一个字段包含JSON格式的一些信息: 1|2|{"StartTime":1572300507000,"EndTime":1547506800474,"DeleteTime":1572217199000}|4 为了检索JSON值,我使用JSON.loads 以下是我的代码的一部分: import sys,json,time with open(sys.argv[1], 'r') as file: for line in file:
1|2|{"StartTime":1572300507000,"EndTime":1547506800474,"DeleteTime":1572217199000}|4
为了检索JSON值,我使用JSON.loads
以下是我的代码的一部分:
import sys,json,time
with open(sys.argv[1], 'r') as file:
for line in file:
fields = line.split('|')
print time.strftime('"%Y%m%d%H%M%S"', time.localtime(json.loads(fields[2])['StartTime'])
这不符合预期,因为纪元时间也有ms。最简单的解决方案是将纪元划分为1000,并执行以下操作:
time.strftime('"%Y%m%d%H%M%S"', time.localtime(json.loads(fields[2])['StartTime']/1000)
这当然不起作用,因为我得到以下错误:
这样做的正确方法是什么?我还试图找到最有效的方法,因为该文件有数百万行。您应该除以该数字,您的错误基于不匹配的括号:
d = '''{"StartTime":1572300507000,"EndTime":1547506800474,"DeleteTime":1572217199000}'''
time.strftime('"%Y%m%d%H%M%S"', time.localtime(json.loads(d)['StartTime']/1000))
输出:
'"20191028230827"'
1.括号不匹配。请显示缺少一个右括号的位置。(在原始代码和带除法的代码中都缺少)。2.错误显示时间已经是结构时间,表明缺少的参数在
/1000
之前?如果是,请将其移到后面。@h4z3指出的似乎是一个不匹配的偏执狂。time.strftime(“%Y%m%d%H%m%S”),(time.localtime(json.loads(fields[2])['StartTime']/1000))
非常感谢Maurice..我发誓我已经尝试过多次了。。
'"20191028230827"'