python - Formatting JSON output -
i have json file key value pair data. json file looks this.
{ "professors": [ { "first_name": "richard", "last_name": "saykally", "helpfullness": "3.3", "url": "http://www.ratemyprofessors.com/showratings.jsp?tid=111119", "reviews": [ { "attendance": "n/a", "class": "chem 1a", "textbook_use": "it's must have", "review_text": "tests incredibly difficult (averages in 40s) , lectures useless. attended both lectures every day , still unable grasp concepts on midterms. scope out gsi , ride curve." }, { "attendance": "n/a", "class": "chemistry1a", "textbook_use": "essential passing", "review_text": "saykally isn't bad made him out be. if go lectures spends half time blowing things up, if read texts before lectures , pay attention he's writing/saying, you'd okay. posts practice tests representative of actual tests , curves class nicely!" }] { { "first_name": "laura", "last_name": "stoker", "helpfullness": "4.1", "url": "http://www.ratemyprofessors.com/showratings.jsp?tid=536606", "reviews": [ { "attendance": "n/a", "class": "ps3", "textbook_use": "you need sometimes", "review_text": "stoker far best professor. if put in effort, take notes, , ask questions, fine in class. far lecture, go bit fast, lecture in form of outline. long take notes, have need exams. funny , super nice if speak her" }, { "attendance": "mandatory", "class": "164a", "textbook_use": "barely cracked open", "review_text": "amazing professor. has way of keeping lectures interesting. yes, can little everywhere , quick lecture, gsi's useful make sure understand material. oh, , did mention she's hilarious!" }] }]
so i'm trying multiple things. i'm trying mentioned ['class'] key under reviews. class name , times mentioned. i'd output format in manner. under professor array. it's info of professors instance chem 1a, chemistry1a - it's richard saykally.
{ courses:[ { "course_name" : # class name "course_mentioned_times" : # amount of times class mentioned professors:[ #the professor array should have professor teaches class in shown json file { 'first_name' : 'professor name' 'last_name' : 'professor last name' } }
so i'd sort json file key-value have max minimum. far i've been able figure out isd
if __name__ == "__main__": open_json = open('result.json') load_as_json = json.load(open_json)['professors'] outer_arr = [] outer_dict = {} items in load_as_json: output_dictionary = {} all_classes = items['reviews'] classes in all_classes: arr_info = [] output_dictionary['class'] = classes['class'] output_dictionary['first_name'] = items['first_name'] output_dictionary['last_name'] = items['last_name'] #output_dictionary['department'] = items['department'] output_dictionary['reviews'] = classes['review_text'] open('output_info.json','wb') outfile: json.dump(output_dictionary,outfile,indent=4)
i think program want:
import json open('result.json') open_json: load_as_json = json.load(open_json) courses = {} professor in load_as_json['professors']: review in professor['reviews']: course = courses.setdefault(review['class'], {}) course.setdefault('course_name', review['class']) course.setdefault('course_mentioned_times', 0) course['course_mentioned_times'] += 1 course.setdefault('professors', []) prof_name = { 'first_name': professor['first_name'], 'last_name': professor['last_name'], } if prof_name not in course['professors']: course['professors'].append(prof_name) courses = { 'courses': sorted(courses.values(), key=lambda x: x['course_mentioned_times'], reverse=true) } open('output_info.json', 'w') outfile: json.dump(courses, outfile, indent=4)
result, using example input in question:
{ "courses": [ { "professors": [ { "first_name": "laura", "last_name": "stoker" } ], "course_name": "ps3", "course_mentioned_times": 1 }, { "professors": [ { "first_name": "laura", "last_name": "stoker" } ], "course_name": "164a", "course_mentioned_times": 1 }, { "professors": [ { "first_name": "richard", "last_name": "saykally" } ], "course_name": "chem 1a", "course_mentioned_times": 1 }, { "professors": [ { "first_name": "richard", "last_name": "saykally" } ], "course_name": "chemistry1a", "course_mentioned_times": 1 } ] }
Comments
Post a Comment