This worksheet focuses on JSON and CSV files, as well as the data structures stack and queue.
As I announced in lecture, this and future worksheets will have a first problem that is treated differently:
The main course materials to refer to for this worksheet are:
csvin the name, and the sample program
(Lecture videos are not linked on worksheets, but are also useful to review while working on worksheets. Video links can be found in the course course Blackboard site.)
In Lecture 17, we wrote a program parentheses.py to examine mathematical expressions like
((1+2)+3) and check that it is possible to match the parentheses up in pairs. It uses a stack to keep track of all the unmatched left parentheses while scanning through the string.
Modify this program (saving the new one as
parentheses2.py) so that while reading an expression, it prints a message every time it encounters a ")" that shows which "(" matches it. The output should look like this:
Expression: (((1+2)/7) + (9/(2+3))) Detected matching pair of parentheses enclosing: (1+2) Detected matching pair of parentheses enclosing: ((1+2)/7) Detected matching pair of parentheses enclosing: (2+3) Detected matching pair of parentheses enclosing: (9/(2+3)) Detected matching pair of parentheses enclosing: (((1+2)/7) + (9/(2+3))) Parentheses matched successfully.
From 2010 to 2012, the National Geographic Channel produced a weekly hour-long TV series titled "Python Hunters". While you might imagine this was a show about computer programming, it was actually about snakes.
A JSON file containing a list of episodes can be found from the link below:
(This is not an official reference, but a user-generated catalog from a web site called TVMaze. They make such datasets available for anyone to use, for free, as long as they give proper attribution.)
You're going to need this data file in this problem, so you should visit the link and save it somewhere (e.g. as
The JSON object in this file is a list, and each item in the list is a dictionary describing an episode . All of the dictionaries have the same keys, which are characteristics of an episode.
The same information could be presented in table form and stored in a CSV file. For example, if the only characteristics of an episode we care about are:
then the episodes could be listed in a CSV file that would look like this:
season,number,name,airdate 1,1,The Perfect Storm,2010-07-12 1,2,The Big Freeze,2010-07-19
Write a Python program
episodes_to_csv.py that reads the JSON file containing the episode list and creates a CSV file in the format described above.
json module to read the JSON file, and use the
csv module to write the CSV file. CSV is simple enough that you could probably write the data in this format manually (i.e. with
file.write()), but the point of this problem is to get some practice using the
Write a program
csvtotaler.py that expects two command line arguments:
sys.argvis the input filename, an existing CSV file
sys.argvis the output filename, which will be created by this program (also CSV)
The CSV file specified as the first argument is expected to contain a header row, followed by columns of integer values. There can be any number of columns, and any number of rows, but all the values are expected to be integers. Here's a sample of what it might look like:
Received,Shipped,Damaged,Returned 291,155,3,8 408,120,5,0 355,109,0,3
The program should read this and write a new CSV file that has all the same columns, as well as a new column called "Rowtype" that appears before the others. Each row of data from the input file should be copied here, with the value "Data" in the "Rowtype" column. Then, the sum of each column should be computed and these columnwise totals written to a row at the bottom whose "Rowtype" is "Total". Thus, for the CSV shown above, the output file would contain:
Rowtype,Received,Shipped,Damaged,Returned Data,291,155,3,8 Data,408,120,5,0 Data,355,109,0,3 Total,1054,384,8,11
Download and save this JSON file containing data about current members of the US House of Representatives.
Now, write a program that reads this and prints the twitter ids of all the representatives from Pennsylvania.
(Do that by exploring the structure of the JSON file yourself, either by loading it into a variable in the REPL or opening the file in an editor and looking around. The point is to practice understanding the layout of information in a JSON file and turning it into code that can access and filter it in Python.)
parentheses2.pyhighlights the parenthesized part of the whole expression, rather than just printing the part inside the parentheses, e.g.
Expression: (((1+2)/7) + (9/(2+3))) Detected matching pair of parentheses: (((1+2)/7) + (9/(2+3))) ^---^ Detected matching pair of parentheses enclosing: (((1+2)/7) + (9/(2+3))) ^-------^ Detected matching pair of parentheses enclosing: (((1+2)/7) + (9/(2+3))) ^---^ Detected matching pair of parentheses enclosing: (((1+2)/7) + (9/(2+3))) ^-------^ Detected matching pair of parentheses enclosing: (((1+2)/7) + (9/(2+3))) ^---------------------^ Parentheses matched successfully.
Make it so that
parentheses2.py shows all parentheses that haven't been closed, rather than just the rightmost one, in case there "(" with no matching ")".
parentheses3.py that shows a text-based bar graph of how many parentheses are open at every point in the expression, like this:
(((1+2)/7) + (9/(2+3))) @@@@@@@@@@@@@@@@@@@@@@@ @@@@@@@@@ @@@@@@@@@ @@@@@ @@@@@
The number of "@" characters appearing directly below any place in the expression tells you how many parentheses are open at that point.