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

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...
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...
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
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
This task is about classes and objects, and is solved in Python 3. We will look...
This task is about classes and objects, and is solved in Python 3. We will look at two different ways to represent a succession of monarchs, e.g. the series of Norwegian kings from Haakon VII to Harald V, and print it out like this: King Haakon VII of Norway, accession: 1905 King Olav V of Norway, accession: 1957 King Harald V of Norway, accession: 1991 >>> Make a class Monarch with three attributes: the name of the monarch, the nation...
Python Programming, Could you also show the right indents in Python Shell. Below are the skeleton...
Python Programming, Could you also show the right indents in Python Shell. Below are the skeleton of the program, and the input and output. # Initialize list mylist = [ ] # Set up loop to put menu on screen num = 1 while num != 0:      print(" ")     print(" ")     print("            Menu ")      print ("0 - Quit")     print ("1 - Add item to list")     print ("2 - Pop item off list and print...
In Python This assignment involves the use of text files, lists, and exception handling and is...
In Python This assignment involves the use of text files, lists, and exception handling and is a continuation of the baby file assignment. You should now have two files … one called boynames2014.txt and one called girlnames2014.txt - each containing the top 100 names for each gender from 2014. Write a program which allows the user to search your files for a boy or girl name and display where that name ranked in 2014. For example … >>>   Enter gender...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT