[英]Print each line of json in new .json file using Python
I have a json
file;我有一个
json
文件; I need to remove the id
key from the content, which I can do with my code.我需要从内容中删除
id
键,我可以用我的代码来做。
Now I want to print each line of the json
file in a new file and use the name filed in my json
for the file name.现在我想在一个新文件中打印
json
文件的每一行,并使用我的json
中的名称作为文件名。
My json
file ex:我的
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}
Code for removing id:删除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()
For deletion you could use something like this:对于删除,你可以使用这样的东西:
import json
import sys
import re
import fileinput
with open('failure.json') as data_file:
data = json.load(data_file)
del data['_id']
with open('failure2.json', 'w') as data_file:
data = json.dump(data, data_file)
and in order to create file with id value, just parse data
object, and value of id
node并且为了创建具有 id 值的文件,只需解析
data
对象和id
节点的值
You sample input show a json
object on each line.您的示例输入在每一行上显示一个
json
对象。
So my solution reads each line and converts it to a python
dict
(using json.loads()
), removes the desired key from the dict
(using dict.pop()
to fail silently if the key is not present) and converts it back to a string (using json.dumps()
), which is then written to the new file.所以我的解决方案读取每一行并将其转换为
python
dict
(使用json.loads()
),从dict
删除所需的键(如果键不存在,则使用dict.pop()
静默失败)并将其转换回来到一个字符串(使用json.dumps()
),然后将其写入新文件。
import json
infile = "failure.json"
outfile = "failure1.json"
key = '_id'
with open(infile) as f_read:
with open(outfile, 'w') as f_write:
for line in f_read:
line = line.strip()
if len(line) > 0:
try:
elem = json.loads(line)
elem.pop(key, None)
f_write.write('{}\n'.format(json.dumps(elem)))
except json.JSONDecodeError:
pass
EDIT: apparently each json
line should go into a separate new file, according to OPs comments.编辑:根据 OP 的评论,显然每个
json
行都应该进入一个单独的新文件。 That could be done like this, for example:可以这样做,例如:
import json
infile = "failure.json"
key_to_remove = '_id'
with open(infile) as f_read:
for line in f_read:
line = line.strip()
if len(line) > 0:
try:
elem = json.loads(line)
elem.pop(key_to_remove, None)
outfile = '{}.json'.format(elem['name']) # this may raise KeyError
with open(outfile, 'w') as f_write:
f_write.write('{}\n'.format(json.dumps(elem)))
except json.JSONDecodeError:
pass
You've imported the json
package, but you're not using it.您已经导入了
json
包,但您没有使用它。 You should, it's great.你应该,这很棒。
Get your string from file and then use json.loads()
to load the string into a json object.从文件中获取字符串,然后使用
json.loads()
将字符串加载到 json 对象中。 From there, you can get each element of the json object with for key in json_object
.从那里,您可以使用
for key in json_object
获取 json 对象的每个元素。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.