Question

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 parameters a year and a category, and returns a sorted list (in normal dictionary order) of the surnames for the winner(s) in that category for that year (up to three people can share the prize). The year will be a string (e.g. "1975"), not a number. The categories are: "chemistry", "economics", "literature", "peace", "physics", and "medicine". The JSON file will be named nobels.json and will be provided - you do not need to submit it.

For example, your class could be used like this:

nd = NobelData()
nd.search_nobel("2001", "economics")

The file must be named: NobelData.py

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

solution:

The main code is below it reads json data into a dictionary of arrays. the output is shown below as well as screenshot is added

CODE:

#import json
import requests #using request sto read data from internet

class NobelData:

def f(self):
return 'hello world'
def __init__(self):
response = requests.get("http://api.nobelprize.org/v1/prize.json")
self.data = response.json()
#print(self.data['prizes'][3])
def search_nobel(self,year,category):
n= len(self.data['prizes'])
winner_names=[]
for i in range(0,n):
if(self.data['prizes'][i]['year']==year and self.data['prizes'][i]['category']==category):
winner_names=self.data['prizes'][i]['laureates']
break
m=len(winner_names)
winner_surnames=[]
for i in range(0,m):
winner_surnames.append(winner_names[i]['surname'])
winner_surnames.sort() #returning sorted list of surnames
return winner_surnames

nd = NobelData()
winner_surnames=nd.search_nobel('2001','economics')
print(winner_surnames)

Output:

['Akerlof', 'Spence', 'Stiglitz']

winner_surnames list [Akerlof, Spence, Stiglitz]

please give me thumb up

Add a comment
Know the answer?
Add Answer to:
python 3 needs to read a local JSON file - it doesn't need to access the...
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...

  • 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...

  • Description This project is an enhancement of your previous project using OOP concepts. You'lI de...

    Please I need help a sample code is provided below of the output Description This project is an enhancement of your previous project using OOP concepts. You'lI define class and instantiate objects instead of using struct to implement your design Write a menu-driven program that provides the following options 1. Show All 2. Spend 3. Search expenses containing this string 4. Search expenses with greater than or equal to this amount 5. Exit It allows the user to select a...

  • These are my answere to the following questions: are they right? 1. B 2. T 3....

    These are my answere to the following questions: are they right? 1. B 2. T 3. T 4. T 5. F 6. T 7. A 8. D 9. E 10. B 11. B 12. A 13. A 14. D 15. C 16. D 17. T 18. C 19. T 20. T 21. T 22. A 23. T 24. D 25. B 26. A 27. A 28. A 29. T 30. C 31. D 32. A 33. T 34. F 35....

  • this is all the information given Personal Financial Planning Mini-Case Jeff and Mary Douglas, a couple...

    this is all the information given Personal Financial Planning Mini-Case Jeff and Mary Douglas, a couple in their mid-30s, have two children - Paul age 6 and Marcy age 7. The Douglas' do not have substantial assets and have not yet reached their peak earning years. Jeff is a general manager of a jewelry manufacturer in Providence, RI while Mary teaches at the local elementary school in the town of Tiverton, RI. The family needs both incomes to meet their...

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