Answered step by step
Verified Expert Solution
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
For this problem, you will need the Unitwnvhumancases.txt file used in the Unit 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.
Butte,
You must use a while loop for reading this file, as shown below.
f openUnitwnvhumancases.txtr # open the file
while True: # loop continuously
line freadline # 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 keyvalue 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.
: Alameda: 'Butte': 'Colusa': 'Contra Costa': El Dorado': 'Fresno': 'Glenn': 'Imperial': 'Kern': 'Kings': 'Lake': 'Los Angeles': 'Marin': 'Merced': 'Modoc': 'Mono': 'Napa': 'Nevada': 'Orange': 'Placer': 'Riverside': 'Sacramento': 'San Bernardino': 'San Diego': 'San Joaquin': 'San Luis Obispo': 'Santa Clara': 'Shasta': 'Solano': 'Stanislaus': 'Sutter': 'Tehama': 'Tulare': 'Ventura': 'Yolo': 'Yuba': : Amador: 'Butte': 'Contra Costa': 'Fresno': 'Glenn': 'Humboldt': 'Kern': 'Los Angeles': 'Madera': 'Merced': 'Orange': 'Placer': 'Riverside': 'Sacramento': 'San Bernardino': 'San Joaquin': 'Shasta': 'Stanislaus': 'Tehama': 'Tulare': 'Tuolumne': 'Ventura': 'Yolo': 'Yuba':
Shown below is partial output. You should see infformation for the years through inclusive.
Expected Output:
records processed from the file
:
countyAlameda cases
countyButte cases
countyColusa cases
countyContra Costa cases
countyEl Dorado cases
countyFresno cases
:
countyAmador cases
countyButte cases
countyContra Costa cases
Problem
Write code that creates the comma separated values text file, called Unitwnvhumancases.txt by iterating over the dictionarywnvDictByYear 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.
Amador,
Butte,
Contra Costa,
Tulare,
Ventura,
Yolo,
Yuba,
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 keyvalue pair then removes that keyvalue 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 # 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
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started