Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Problem 9 For this problem, you will need the Unit 0 4 _ wnvhumancases.txt file used in the Unit 4 problem set. This file contains

Problem 9
For this problem, you will need the Unit04_wnvhumancases.txt file used in the Unit 4 problem set. This file contains information about West Nile virus cases in California. This file consists of multiple columns separated by commas: year,week,county,number of cases. Below is a line from the file. For this problem you will need the year, county, and number of cases.
2006,33,Butte,4
You must use a while loop for reading this file, as shown below.
f = open('Unit04_wnvhumancases.txt','r') # open the file
while True: # loop continuously
line = f.readline() # read the next line in the file
if not line: # if there are no more lines
break. # exit the loop
The objective of the problem is to read the data from the file and build a dictionary called wnvDictByYear (or whatever you choose) that summarizes the data by year and county. As implied by the name, the key for wnvDictByYear is the year. The value is a dictionary whose key is the county name and the value the associated number of cases. You will then print the information in wnvDictByYear as shown in expected output. Below is a suggested approach. However, you may choose a different approach provided that a summary dictionary (described by wnvDictByYear above) is built and printed as shown below.
Approach:
Open the file for reading.
Initialize wnvDictByYear and other variables.
Use a while statement to loop continuously.
Read a line from the file.
If no more lines in the file, exit the loop
increment the line counter
Split the line into its parts: year, week, county, #cases.
If the year is already in wnvDictByYear:
Retrieve the value associated with the year into yearDict.
If the county is in yearDict:
add #cases to the value
otherwise:
Add a new entry to yearDict using the county as the key and #cases as the value.
otherwise:
initialize the empty dictionary yearDict
Add the key/value pair county,#cases to yearDict.
Add a new entry to wnvDictByYear using the pair year,yearDict.
Close the file..
Print the output .
Observe that this problem uses the standard pattern for building a dictionary. Within that pattern, we are using the same pattern to build the nested dictionary.
If you built the dictionary correctly, it should appear, when printed, similar to the partial listing below.
{2006: {'Alameda': 1, 'Butte': 31, 'Colusa': 3, 'Contra Costa': 8,'El Dorado': 2, 'Fresno': 11, 'Glenn': 12, 'Imperial': 1, 'Kern': 49, 'Kings': 1, 'Lake': 2, 'Los Angeles': 12, 'Marin': 1, 'Merced': 4, 'Modoc': 2, 'Mono': 1, 'Napa': 1, 'Nevada': 1, 'Orange': 6, 'Placer': 8, 'Riverside': 4, 'Sacramento': 15, 'San Bernardino': 3, 'San Diego': 1, 'San Joaquin': 8, 'San Luis Obispo': 1, 'Santa Clara': 5, 'Shasta': 4, 'Solano': 6, 'Stanislaus': 11, 'Sutter': 12, 'Tehama': 3, 'Tulare': 8, 'Ventura': 3, 'Yolo': 26, 'Yuba': 5},....2018: {'Amador': 1, 'Butte': 6, 'Contra Costa': 1, 'Fresno': 3, 'Glenn': 2, 'Humboldt': 1, 'Kern': 5, 'Los Angeles': 11, 'Madera': 2, 'Merced': 1, 'Orange': 1, 'Placer': 7, 'Riverside': 7, 'Sacramento': 1, 'San Bernardino': 3, 'San Joaquin': 9, 'Shasta': 1, 'Stanislaus': 6, 'Tehama': 1, 'Tulare': 1, 'Tuolumne': 1, 'Ventura': 1, 'Yolo': 6, 'Yuba': 1}}
Shown below is partial output. You should see infformation for the years 2006 through 2018 inclusive.
Expected Output:
1789 records processed from the file
2006 :
county=Alameda cases=1
county=Butte cases=31
county=Colusa cases=3
county=Contra Costa cases=8
county=El Dorado cases=2
county=Fresno cases=11
...
2018 :
county=Amador cases=1
county=Butte cases=6
county=Contra Costa cases=1
...
Problem 10
Write code that creates the comma separated values text file, called Unit06_wnvhumancases.txt, by iterating over the dictionary(wnvDictByYear) created in the previous problem. Each line in the file must consist of three columns: year, county, and number of cases for that county. Below is an example showing some of the records. Do not be concerned about the order of the records in the file.
2018,Amador,1
2018,Butte,6
2018,Contra Costa,1
...
2006,Tulare,8
2006,Ventura,3
2006,Yolo,26
2006,Yuba,5
You need to use a while loop to iterate over the dictionary. The example below provides the necessary syntax:
while wnvDictByYear:
year, yearDict = wnvDictByYear.popitem()
The popitem method returns the next key/value pair then removes that key/value pair from the dictionary. This means that once the dictionary is emptied the loop terminates. The implication is that if your code is rerun it would not produce any output because wnvDictByYear is empty. You must make your code repeatable without requiring rerunning the previous problem to rebuild wnvDictByYear. You may not copy the code from #9 to build wnvDictByYear. This is not as evident as you may think. You will need to do some research to find a solution.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Concepts

Authors: David Kroenke

4th Edition

0136086535, 9780136086536

More Books

Students also viewed these Databases questions