Question: Extra Credit Step - 1 0 points While tab delimited text files are handy, JSON is a more elegant way to go . JSON allows
Extra Credit Step points
While tab delimited text files are handy, JSON is a more elegant way to go JSON allows you to 'serialize' your data structures into a string and dump it to a file or send it over a network. The JSON data can then be loaded back from the file and deserialized into the original data structures. Rather than formatting our data to write it to a tab delimited text file, instead we'll encapsulate it as a dictionary element and dump it to a JSON file. The json module does the heavy lifting for us
Copy steppy to steppy as a starting point.
import the json module
Since JSON files use a json' file name extension, change the file name to be opened from "stepdata.txt to "forum.json".
Modify the 'else' block of the tryexceptelse to remove the text file processing and instead call json.load with the open file object and store the results in a variable, which we can call ds Note that the results are a dictionary, with a single item named "records" and a records list as a value. Overwrite your records list with the value from dsrecords Remember to close the file.
When the while loop exits, remove the code for writing to the text file, but leave the call to open the file for writing.
Recreate the ds dictionary with a single item. The item's key should be "records" and it's value should be your records list.
Call json.dump, passing ds and your file object as arguments.
Remember to close the file.
Test your work as you did for Step
If all goes well, give yourself a pat on the back. Well done!
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
