Introduction

This assignment lets your practice with reading from a file and manipulating lists.

Preparation

This assignment must be done in a standalone Python environment.

1. Download the following all to the same folder:

  • The program Lab9-start.py
  • The data files health-no-head-sample.csv and health-no-head.csv
    • The big data file contains records of some infectious diseases from 1928 to 2011. The small one only includes data from 3 years from 5 states. Run the python program. It should print something like this
MEASLES,206.98,COLORADO,2099,1014000,1928

['MEASLES', '206.98', 'COLORADO', '2099', '1014000', '1928n']
MEASLES,634.95,CONNECTICUT,10014,1577000,1928

['MEASLES', '634.95', 'CONNECTICUT', '10014', '1577000', '1928n']
MEASLES,656.02,DELAWARE,597,233000,1928

['MEASLES', '656.02', 'DELAWARE', '597', '233000', '1928n']
...

2. Make sure that you get output like this before starting the assignment or writing any additional code.

Directions

Modify the program in the following ways:

1. Write each line as part of a table, include a header before the table, and a summary line at the end. Use a fixed width for each column (don't try to find the largest width like you did in the previous unit). You should end up with something like

State                 Disease   Number  Year

COLORADO MEASLES 2,099 1928
CONNECTICUT MEASLES 10,014 1928
DELAWARE MEASLES 597 1928
...
DELAWARE SMALLPOX 0 1930
DISTRICT OF COLUMBIA SMALLPOX 0 1930
FLORIDA SMALLPOX 28 1930
Total 52,307

Not every field of the original line is used in the output. You will have to do some research about the .format() function to print the number of cases with a comma. If you can't get the comma in the number column, move on and come back on that once you have more of the program written. The key is to have all the columns line up.

2. Use some if statements to add three filters to your program that let the user select exactly one state, disease and year to include in the report. Prompt the user to enter these values.

Enter state: Colorado
Enter disease: smallpox
Enter year: 1928

State Disease Number Year

COLORADO SMALLPOX 340 1928
Total 340

Unfortunately, this isn't very flexible.

3. Change your program so that if the user just hits return for a prompt, the program includes all the data for that field. For example:

Enter state (Empty means all): Colorado
Enter disease (Empty means all):
Enter year (Empty means all): 1928

State Disease Number Year

COLORADO MEASLES 2,099 1928
COLORADO POLIO 71 1928
COLORADO SMALLPOX 340 1928
Total 2,510

Your program should run as expected using the small data set

4. Change the open statement in the program to use the full data set, health-no-head.csv.

5. Write down the answers to the following queries:

  • How many cases of Hepatitis A were reported in Utah in 2001?
  • How many cases of polio have been reported in California?
  • How many cases of all diseases were reported in 1956?

6. Add another feature to your program.

This could be something like printing the highest and lowest numbers for each query, or allowing the user to just type the first part of value, so that entering 20 for the year generates a table for years 2000, 2001, 2002, 2011, or entering D for a state gives information on Delaware and the District of Columbia. Or maybe leverage your previous assignment and make the column only as wide as they need to be for the data. Try to make it something useful.

7. Comment on your code.

Be sure to add a header comment to your program and any functions, and appropriate comments before other blocks of code.

Starter Codes

lab9-start.py

# Open and read health data file one line at a time
# Columns are
# disease,increase,location,number,population,year

file = open("health-no-head-sample.csv", "r")


# Process each line of the file
for aline in file:
print (aline)
values = aline.split(',')
print (values)

# Close file
file.close()

health-no-head-sample.csv

MEASLES,206.98,COLORADO,2099,1014000,1928
MEASLES,634.95,CONNECTICUT,10014,1577000,1928
MEASLES,256.02,DELAWARE,597,233000,1928
MEASLES,535.63,DISTRICT OF COLUMBIA,2566,479000,1928
MEASLES,119.58,FLORIDA,1714,1433000,1928
POLIO,7.04,COLORADO,71,1014000,1928
POLIO,4.53,CONNECTICUT,72,1577000,1928
POLIO,3.44,DELAWARE,8,233000,1928
POLIO,6.92,DISTRICT OF COLUMBIA,33,479000,1928
POLIO,1.47,FLORIDA,21,1433000,1928
SMALLPOX,33.58,COLORADO,340,1014000,1928
SMALLPOX,10.19,CONNECTICUT,161,1577000,1928
SMALLPOX,0.86,DELAWARE,2,233000,1928
SMALLPOX,2.1,DISTRICT OF COLUMBIA,10,479000,1928
SMALLPOX,10.99,FLORIDA,157,1433000,1928
MEASLES,74.24,COLORADO,748,1008000,1929
MEASLES,614.82,CONNECTICUT,9800,1594000,1929
MEASLES,239.82,DELAWARE,566,236000,1929
MEASLES,94.2,DISTRICT OF COLUMBIA,455,483000,1929
MEASLES,78.01,FLORIDA,1127,1445000,1929
POLIO,1.3,COLORADO,13,1008000,1929
POLIO,1.32,CONNECTICUT,21,1594000,1929
POLIO,2.1,DELAWARE,5,236000,1929
POLIO,1.26,DISTRICT OF COLUMBIA,6,483000,1929
POLIO,2.38,FLORIDA,34,1445000,1929
SMALLPOX,83.77,COLORADO,844,1008000,1929
SMALLPOX,2.69,CONNECTICUT,43,1594000,1929
SMALLPOX,0.84,DELAWARE,2,236000,1929
SMALLPOX,0,DISTRICT OF COLUMBIA,0,483000,1929
SMALLPOX,2.52,FLORIDA,36,1445000,1929
MEASLES,1132.76,COLORADO,11781,1040000,1930
MEASLES,112.23,CONNECTICUT,1810,1613000,1930
MEASLES,109.25,DELAWARE,261,239000,1930
MEASLES,182.1,DISTRICT OF COLUMBIA,889,488000,1930
MEASLES,356.59,FLORIDA,5245,1471000,1930
POLIO,6.25,COLORADO,65,1040000,1930
POLIO,4.57,CONNECTICUT,74,1613000,1930
POLIO,2.52,DELAWARE,6,239000,1930
POLIO,1.8,DISTRICT OF COLUMBIA,9,488000,1930
POLIO,0.84,FLORIDA,12,1471000,1930
SMALLPOX,54.03,COLORADO,562,1040000,1930
SMALLPOX,0,CONNECTICUT,0,1613000,1930
SMALLPOX,0,DELAWARE,0,239000,1930
SMALLPOX,0,DISTRICT OF COLUMBIA,0,488000,1930
SMALLPOX,1.94,FLORIDA,28,1471000,1930
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.