Question

In: Computer Science

Python programming ***************************************************************************************************** If we load the json files then how could we use the objects...

Python programming

*****************************************************************************************************

If we load the json files then how could we use the objects to extract particular values from those files.

eg:

fi1 = open("king.json", "rb")
obj1 = yaml.safe_load(fi1.read())
fi1.close()

fi2 = open("queen.json", "rb")
obj2 = yaml.safe_load(fi2.read())
fi2.close()

Now if the JSON file queen.json had elements like name, rule year, children, etc. and we wanted the name of the queen who ruled between particular years, how would we do it?

second would be getting a value common for both files? like the name of king and queen who ruled between specific years.

Solutions

Expert Solution

Please find below code in Python and Don't forget to give a Like.

Please refer screenshot for indentation and output and also json files i used.

Code:

import json
year=int(input("Enter the year"))
from_year,to_year=input("enter from year, to year with spaces: ").split()
from_year=int(from_year)
to_year=int(to_year)
fil2=open("queen.json",'r')
object2=json.load(fil2)
list1=[],list2=[]
for i in object2:
    if(i['rule_year']==year):
        print("The name of the Queen for particular year is ",i['name'])
    if(i['rule_year']>=from_year and i['rule_year']<=to_year):
        list1.append(i['name'])
print("The names of queen for specific years",from_year,"to",to_year)
for i in list1:
    print(i)
fil2.close()
fil1=open("king.json",'r')
object1=json.load(fil1)
for i in object1:
    if(i['rule_year']>=from_year and i['rule_year']<=to_year):
        list2.append(i['name'])
print("The names of king for specific years",from_year,"to",to_year)
for i in list2:
    print(i)
fil1.close()

Queen.json file

King.json file


Related Solutions

This is Python programming Focus 1. Classes and Objects 2. Creating objects 3. Manipulating objects This...
This is Python programming Focus 1. Classes and Objects 2. Creating objects 3. Manipulating objects This lab maps to learning the following objectives: Write a working program that creates a Class, Instances of Objects from that Class, and Functions that use Objects as parameters. For this portion of the lab, you will create a new program for your Professor. Create a class named Student that holds the following data about a student: 1. Name 2. Student ID number 3. GPA...
Programming language is in python 3 For this project, you will import the json module. Write...
Programming language is in python 3 For this project, you will import the json module. 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...
Programming language is python 3 For this project, you will import the json module. Write a...
Programming language is python 3 For this project, you will import the json module. Write a class named NeighborhoodPets that has methods for adding a pet, deleting a pet, searching for the owner of a pet, saving data to a JSON file, loading data from a JSON file, and getting a set of all pet species. It will only be loading JSON files that it has previously created, so the internal organization of the data is up to you. The...
2. Combine multiple files Use Python programming to combine two text files: customer-status.txt and sales.txt Data...
2. Combine multiple files Use Python programming to combine two text files: customer-status.txt and sales.txt Data columns in customer-status.txt (separated by comma): Account Number, Name, Status 527099,Sanford and Sons,bronze Data columns in sales.txt (separated by comma): Account Number, Name, SKU, Quantity, Unit Price, Ext Price, Date 163416,Purdy-Kunde,S1-30248,19,65.03,1235.57,2014-03-01 16:07:40 527099,Sanford and Sons,S2-82423,3,76.21,228.63,2014-03-01 17:18:01 After you combine, you will see the following: 527099,Sanford and Sons,S2-82423,3,76.21,228.63,2014-03-01 17:18:01,bronze
Python programming problem! Suppose that there is a json file called data from the desktop. I...
Python programming problem! Suppose that there is a json file called data from the desktop. I want to print the value with the key of "year" and "country". Json file below: { "A":{ "year":11, "grade":A, "country":America}, "B":{ "year":18, "grade":C, "country":England}, "C":{ "year":19, "grade":B, "country":China},} I want a code that I can only replace the name of key from year to grade and the code could be work.
how do we use dictionaries to get json api in MIT app inventor
how do we use dictionaries to get json api in MIT app inventor
Now we will analyze the truck and load as two individual objects that interact with each...
Now we will analyze the truck and load as two individual objects that interact with each other as they accelerateleft:2)We’ll start with analyzing the load.a.You discuss what forces are acting on the load with some fellow students. Everyone agrees that the Earth exerts a downward force on the load, but there is some disagreement about any other forces acting on the load. Use⃑as the force which the truck exerts on the load. The vertical component of ⃑is often given a...
Use Python to Load a file containing a list of words as a python list :param...
Use Python to Load a file containing a list of words as a python list :param str filename: path/name to file to load :rtype: list
how can we use cryptography libraries in Python
how can we use cryptography libraries in Python
In Python For this programming assignment, we are going to investigate how much "work" different sorting...
In Python For this programming assignment, we are going to investigate how much "work" different sorting routines do, based on the input size and order of the data. We will record the work done by writing output CSV (comma separated value) files and creating various plots using matplotlib. Note: for this assignment, do not use Jupyter Notebook to code your solution. Use standard .py files and save your output to .csv and .png files (see the program details below for...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT