Question
***Using Python 3*** Write a class named SatData that reads a JSON file containing data on 2010 SAT results for New York City and writes
***Using Python 3*** Write a class named SatData that reads a JSON file containing data on 2010 SAT results for New York City and writes the data to a text file in CSV (comma-separated values) format. It just needs to read a local JSON file - it doesn't need to access the internet. Specifically, your class should have an init method that reads the file, and it should have a method named save_as_csv that takes as a parameter a list of DBNs (district bureau numbers) and saves a CSV file that looks like this, but with only the rows corresponding to the DBNs in the list (and also the row of column headers). You may assume that the DBNs in the list are all present in the JSON file. The rows in the CSV file must be sorted in ascending order by DBN. The name of the file must be output.csv. There is a csv module for Python, but you will not use it for this project. CSV is a very simple format - just commas separating columns and newlines separating rows. The JSON file will be named sat.json and will be provided - you do not need to submit it.
For example, your class could be used like this:
sd = SatData() dbns = ["02M303", "02M294", "01M450", "02M418"] sd.save_as_csv(dbns)
Some of the school names contain one or more commas. How should you preserve such a name as a single field in CSV format, since commas normally separate fields? The correct way to handle that is to enclose such names in double quotes in your CSV file. If you open up the example CSV file in a text editor, you can see that's what it does.
The file must be named: SatData.py
This is what i have so far:
import json class SatData: def __init__(self): with open('sat.json', 'r') as infile: self._data = json.load(infile) def save_as_csv(self, dbns): columns = 'DBN', 'School Name', 'Number of Test Takers', 'Critical Reading Mean', 'Mathematics Mean', 'Writing Mean' with open('output.csv', 'w') as outfile: for i in columns: outfile.write(' ' + i[8] + ',' + i[9] + ' ' + i[10] + ',' + i[11] + ' ' + i[12] + ',' + i[13]) json.dump(dbns, outfile)
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started