Issue
I have 100 json file, each json file contains following kind of dict format.
I would like to create a csv file and dump only
{
"label": "image",
"confidence": 1.0
}
this data into csv file into prediction column along with json file name. How would I do it?
Solution
rowIf I understand ok what you want, you want to get only the first "item" on predictions list, from multiple files on some path. And then put all this as rows on a csv. So you can do something like:
import csv
import json
from os import listdir
from os.path import isfile, join
path = 'path/to/dir'
result = []
for file_name in listdir(path):
with open(join(path, file_name), 'r') as f:
data = json.load(f)
first = data['predictions'][0]
result.append([first['label'], first['confidence']])
with open('path/to/result.csv', 'w', newline='') as f:
writer = csv.writer(f)
writer.writerow(['label', 'confidence']) # Comment this line if you dont want a header row
writer.writerows(result)
Replacing 'path/to/dir'
with the path of the json files directory, and 'path/to/result.csv'
with the path to the result csv file.
Answered By - Rodrigo Llanes
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.