我有一个json文件;我需要id从内容中删除密钥,我可以用我的代码来做。
现在我想json在一个新文件中打印文件的每一行,并使用 myjson中的名称作为文件名。
我的json文件前:
{"categories":["Test"],"indications":[{"@class":"=indication.BuildLogIndication","pattern":".*TypeError .*"},{"@class":"model.indication.BuildLogIndication","pattern":".*LoadError .*"}],"modifications":[{"time":{"$date":"2015-10-08T20:01:54.075Z"}},{"user":"user1","time":{"$date":"2015-03-04T18:38:58.123Z"}},{"user":"user2","time":{"$date":"2014-11-13T01:54:13.906Z"}},{"time":{"$date":"2014-09-02T18:48:05.000Z"}}],"lastOccurred":{"$date":"2017-01-25T20:05:17.180Z"}}
{"pattern":".*look for this string.*"}],"modifications":[{"time":{"$date":"2014-09-02T18:52:20.000Z"}}],"lastOccurred":{"$date":"2014-11-04T00:43:32.945Z"},"_removed":{"timestamp":{"$date":"2014-11-13T01:52:44.346Z"},"by":"user3"},"active":false}
删除id的代码:
import json
import sys
import re
import fileinput
infile = "failure.json"
outfile = "failure1.json"
fin = open(infile)
fout = open(outfile, "w+")
for line in fin:
for word in line:
line = re.sub("\"_id.*?},","", line)
fout.write(line)
file.write("%d\n" % n)
fin.close()
fout.close()
心有法竹
芜湖不芜
一只斗牛犬
相关分类