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 -10 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 de-serialized 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.
1. Copy step_3.py to step_4.py as a starting point.
2. import the json module
3. Since JSON files use a '.json' file name extension, change the file name to be opened from "step3_data.txt" to "forum.json".
4. Modify the 'else' block of the try/except/else 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 ds["records"]. Remember to close the file.
5. When the while loop exits, remove the code for writing to the text file, but leave the call to open the file for writing.
6. Re-create the ds dictionary with a single item. The item's key should be "records" and it's value should be your records list.
7. Call json.dump, passing ds and your file object as arguments.
8. Remember to close the file.
9. Test your work as you did for Step 3.
10. 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 blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!