Question

In: Computer Science

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.

Solutions

Expert Solution

import json 

fName = input('Enter json file name: ')
lines = open(fName).readlines()
lines = [l.strip() for l in lines if l.strip() != '']
content = ' '.join(lines)

dataDict = json.loads(content)

# You cna print below to check the dict content
#print(dataDict)

# now you want to print the value for key 'year'
for (x, y) in dataDict.items():
    print(y['year'])
    print(y['country'])

# to change the key in dict object
for (x, y) in dataDict.items():
    print(y['year'])
    print(y['country'])


# to change the key in dict object
for (x, y) in dataDict.items():
    # change year key to year1
    y['year1'] = y['year']
    del y['year']

print(dataDict)

===========================

Your json file is incorrect. That is not a valid json. The strigns should be enclosed in quotes..
The valid json is below:

{
"A":{
"year":11,
"grade":"A",
"country":"America"},
"B":{
"year":18,
"grade":"C",
"country":"England"},
"C":{
"year":19,
"grade":"B",
"country":"China"}}

================


Please upvote, as i have given the exact answer as asked in question. Still in case of any concerns in code, let me know in comments. Thanks!


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...
i need to post a file to a url. The "Content-Type: multipart/form-data" and it "accept: application/json"...
i need to post a file to a url. The "Content-Type: multipart/form-data" and it "accept: application/json" i need to upload a file, three strings. It needs to be in Java, but i keep getting issues.
Python Assignment You will be writing an inventory system that is backed by JSON data and...
Python Assignment You will be writing an inventory system that is backed by JSON data and will be working with a starter file that contains a JSON string. The code you write will need to follow the following guidelines. The what You’re at work one day and your boss asks you about that fancy programming language you’ve been learning, Python. She asks you if you can use it to read JSON data from a supplier and build an inventory. “Of...
Using C programming I have a file that contains earthquake data that I will copy and...
Using C programming I have a file that contains earthquake data that I will copy and paste below. I want to use either bubble or insertion sort to sort the file by latitude in ascending order, then create a new file containing the sorted data. example file to sort: time,latitude,longitude,depth,mag,magType,nst,gap,dmin,rms,net 2020-10-17T17:22:03.840Z,32.877,-116.2991667,0.31,1.16,ml,21,119,0.07747,0.26,ci 2020-10-17T17:17:29.980Z,34.1611667,-116.452,2.75,0.87,ml,17,66,0.05224,0.22,ci 2020-10-17T17:03:54.460Z,33.5396667,-116.4613333,8.66,0.63,ml,18,126,0.06084,0.16,ci 2020-10-17T16:55:01.080Z,63.254,-151.5232,8,1.4,ml,,,,0.9,ak
Write a python program: There is a file called file 2. File2 is a txt file...
Write a python program: There is a file called file 2. File2 is a txt file and I have written the contents of file 2 below in the exact format it was in notepad. # This comment does not make sense # It is just to make it harder # The job description starts after this comment, notice that it has 4 lines. # This job description has 700150 hay system points\\ the incumbent will administer the spending of kindergarden...
This problem is adapted from Exercise 37 of Langtangen’s “A Primer on Scientific Programming with Python”....
This problem is adapted from Exercise 37 of Langtangen’s “A Primer on Scientific Programming with Python”. Simulating gas particles in a closed space is something you can do with a Monte Carlo type simulation – simulate a period of time (using a loop) and “move” particles around (change values for (x,y) coordinates) for each unit of time. This kind of simulation is called a “random walk”. Download the file randomwalk.py(i posted it below). This implements a Monte Carlo simulation for...
This is all in Python. Also, the names come from a .txt file that i have...
This is all in Python. Also, the names come from a .txt file that i have created with 3 names on it(Jim,Pam,Dwight). Main Function. Write a program where the user enters a name. Using the read function, check to see if the name is in the text document. If it is, respond back to the user the name is found within the list. Read Function. The read function should return the contents of the text document as a list. EXAMPLE...
Using Python create a script called create_notes_drs.py. In the file, define and call a function called...
Using Python create a script called create_notes_drs.py. In the file, define and call a function called main that does the following: Creates a directory called CyberSecurity-Notes in the current working directory Within the CyberSecurity-Notes directory, creates 24 sub-directories (sub-folders), called Week 1, Week 2, Week 3, and so on until up through Week 24 Within each week directory, create 3 sub-directories, called Day 1, Day 2, and Day 3 Bonus Challenge: Add a conditional statement to abort the script if...
Python programming Summary Store the times into arrays called Chevy[ ] and Ford[ ]. Then list...
Python programming Summary Store the times into arrays called Chevy[ ] and Ford[ ]. Then list the winner of each pair, giving the number of seconds the winner won by. At the end declare which team won based on which team had the most wins. Lab Steps There are eight cars in each team called Chevy and Ford. One car from each team races its opponent on the drag strip. Read in the racing times for the eight Chevy cars...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT