Question

Write a class named SatData that reads a JSON file containing data on 2010 SAT results...

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

MUST BE WRITTEN IN PYTHON!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Hi Friend,

I tooked a dummy json file to create the code for your problem, whoes format is given below:

******** JSON FORMAT **********

{}JSON +{}meta []data [] 0:row-4fb2.h7ir.w2nm 1:00000000-0000-0000-47D2-278BF31BBA1F 2:0 3: 1425759032 4: null 5: 1425759

I have inline comments in the code for the ease of your understanding

********** CODE *******************

import json
class SatData:
def __init__(self):
#Read the json file
with open('test_json.json','r') as f:
self.data = json.load(f)['data']
def save_as_csv(self,dbns):
resData=[]#to store the required dbns data
#Fetch the data of the required dbns
for data in self.data:
if data[8] in dbns: #8assuming dbns is located at position 8
resData.append(data)
#Sorting the dbns data based upon the dbns
resData = sorted(resData,key=lambda x:x[8])

#Create output.csv to store the reult
with open('output.csv','a+') as f:
#Add the row of column header
header = [ str(i) for i in range(len(resData[0]))]
f.write(','.join(header))
#Create new row
f.write('\n')
#Add the remaining data
for row in resData:
rowData=[]
for item in row:
#To handle the entry which has , in between
if ',' in str(item):
rowData.append("\""+item+"\"")
else:
rowData.append(str(item))
f.write(','.join(rowData))
#Create new row
f.write('\n')
  
sd = SatData()
dbns = ["02M303", "02M294", "01M450", "02M418"]
sd.save_as_csv(dbns)

******* SNAPSHOT *****************

import json class SatData: definit__(self): #Read the json file with open(test_json.json, r) as f: self.data = json.load(

Output

H M 4 6 B 10 row-37g8. 00000000- row-qywq 00000000- row-yvp3_00000000- row-b79e. 00000000- D E 2 3 B 0 1.43E+09 None 0 1.43E+

I hope it helps

Please get back to me in comment section in case of any query

Have a great day

Add a comment
Know the answer?
Add Answer to:
Write a class named SatData that reads a JSON file containing data on 2010 SAT results...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a class named NobelData that reads a JSON file containing data on Nobel Prizes and...

    Write a class named NobelData that reads a JSON file containing data on Nobel Prizes and allows the user to search that data. Specifically, your class should have an init method that reads the file, and it should have a method named search_nobel that takes as parameters a year and a category, and returns a sorted list of the surnames for the winner(s) in that category for that year (up to three people can share the prize). The year will...

  • python 3 needs to read a local JSON file - it doesn't need to access the...

    python 3 needs to read a local JSON file - it doesn't need to access the internet. Write a class named NobelData that reads a JSON file containing data on Nobel Prizes and allows the user to search that data. 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 search_nobel that takes as...

  • Using C# or Python, write a code that reads a Microsoft Excel CSV file named "LabAssignment"...

    Using C# or Python, write a code that reads a Microsoft Excel CSV file named "LabAssignment" then outputs all the collected and formated data as another Microsoft Excel CSV file named "labOutput" The CSV file contains 25 colums as well close to 200 rows of data. Write a code in C# or Python so that: It removes all unwanted columns along with its data...the columns that should be kept have the following header: "User" "Location" "Feature" "HoursUsed" Remove all files...

  • write a function: Names(path): that reads from a file at location path and returns a dictionary...

    write a function: Names(path): that reads from a file at location path and returns a dictionary mapping from column names to lists containing the data in those columns. The format of the file is a csv file. The first line is a comma separated set of string names for the columns contained in the file. The function will accumulate a dictionary mapping from year to the list of years, from name to the list of names (in the same order),...

  • According to Wikipedia , a comma-separated values (CSV) file is a delimited text file that uses...

    According to Wikipedia , a comma-separated values (CSV) file is a delimited text file that uses a comma to separate values. A CSV file stores tabular data (numbers and text) in plain text. Each line of the file is a data record. Each record consists of one or more fields, separated by commas. The use of the comma as a field separator is the source of the name for this file format. A company has text data that is not...

  • The way I understand it is i'm trying to link a list that I read into...

    The way I understand it is i'm trying to link a list that I read into python from a cvs file to json and xml and pass the doctest. Please refere the lines where I show what I did below. home / study / engineering / computer science / questions and answers / """this script converts a csv file with headers ... Question: """This script converts a CSV file with headers to... Bookmark """This script converts a CSV file with...

  • project-8c Write a class named Employee that has data members for an employee's name, ID_number, salary,...

    project-8c Write a class named Employee that has data members for an employee's name, ID_number, salary, and email_address (you must use those names - don't make them private). Write a function named make_employee_dict that takes as parameters a list of names, a list of ID numbers, a list of salaries and a list of email addresses (which are all the same length). The function should take the first value of each list and use them to create an Employee object....

  • Write you code in a class named HighScores, in file named HighScores.java. Write a program that...

    Write you code in a class named HighScores, in file named HighScores.java. Write a program that records high-score data for a fictitious game. The program will ask the user to enter five names, and five scores. It will store the data in memory, and print it back out sorted by score. The output from your program should look approximately like this: Enter the name for score #1: Suzy Enter the score for score #1: 600 Enter the name for score...

  • Write a program that first reads in the name of an input file and then reads the file using the csv.reader() method.

     Write a program that first reads in the name of an input file and then reads the file using the csv.reader() method. The file contains a list of words separated by commas. Your program should output the words and their frequencies (the number of times each word appears in the file) without any duplicates. Ex: If the input is: inputl.csv and the contents of input1.csv are: hello, cat, man, hey, dog, boy, Hello, man, cat, woman, dog, Cat, hey, boy the output is: hello 1 cat 2 man...

  • Create a java program that reads an input file named Friends.txt containing a list 4 names...

    Create a java program that reads an input file named Friends.txt containing a list 4 names (create this file using Notepad) and builds an ArrayList with them. Use sout<tab> to display a menu asking the user what they want to do:   1. Add a new name   2. Change a name 3. Delete a name 4. Print the list   or 5. Quit    When the user selects Quit your app creates a new friends.txt output file. Use methods: main( ) shouldn’t do...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT