我有一个用管道分隔的文件,其中一个字段包含一些JSON格式的信息:
1|2|{"StartTime":1572300507000,"EndTime":1547506800474,"DeleteTime":1572217199000}|4
为了检索JSON值,我正在使用json.loads。
Bellow是我的代码的一部分:
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)
这当然不起作用,因为出现以下错误:
TypeError: unsupported operand type(s) for /: 'time.struct_time' and 'int'
哪个是正确的方法?我正在尝试寻找最有效的方法,因为该文件具有数百万行。