-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask2.py
More file actions
43 lines (32 loc) · 1.56 KB
/
task2.py
File metadata and controls
43 lines (32 loc) · 1.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import json
import csv
def task2():
# Define the rows and column titles of thr required csv file to be outputted
fields = ['news_id', 'news_title', 'review_title', 'rating', 'num_satisfactory']
rows = []
# Open the json file containing details about the reviews and load the data
f = open('/course/data/a1/reviews/HealthStory.json')
data = json.load(f)
# Loop through each individual review in the json file
for individual_review in data:
# Allocate the required data within each review to seperate variables
news_id = individual_review['news_id']
news_title = individual_review['original_title']
review_title = individual_review['title']
rating = individual_review['rating']
# Count the total number of satisfactory criteria for each review
num_satisfactory = 0
# Loop through each category and see if it has been marked as "Satisfactory"
for category in individual_review['criteria']:
if category['answer'] == "Satisfactory":
num_satisfactory += 1
# Add each row in the form of a list and aggregate all rows into a single nested list
rows.append([news_id, news_title, review_title, rating, num_satisfactory])
# Sort the rows in ascending order of news_id
rows.sort(key=lambda x: x[0])
# Create task2.csv and write the rows and columns to it
with open('task2.csv', 'w') as csvfile:
csvwriter = csv.writer(csvfile)
csvwriter.writerow(fields)
csvwriter.writerows(rows)
return