In this assignment, you will be demonstrating your understanding and use of python constructs, as covered in module 2.

This assignment is design to help you practice your skills at working with files. You will read data from a CSV file and, following input from the user, save view of the data to a CSV file.

NOTE: You are required to use the given dataset found in the attached file named:

1. Let’s read the data into memory.
a. Using what you have learned in the past 2 modules, create a load_census_data function.
i. The function must use with to open the file
ii. Using the csv module you must read the contents of the file
iii. Verify
1. that each row has 7 keys (the number of columns)
2. that each of the column names (keys) is found in each row
3. that all values are ints
iv. Place each validated row (from iii) into a list.
v. Any row that does not have valid data must be discarded (not added to the list)
vi. The function must return a tuple by converting the list into a tuple
2. Invoke load_census_data to get the data stored in the CSV file by passing the path to the file to the load_census_data function. It is OK to hard code the path to the CSV file.
a. Keep a reference to the resulting tuple by creating a variable
3. Now that we have data let’s ask the user what data they want to retrieve from the data set:
a. Create a tuple of all the column names
b. Enumerate all the column names and display to the user.
For example
1) Zip Code
2) Total Population
3) Median Age
4) Total Males
5) Total Females
6) Total Households
7) Average Household Size
c. ask the user which column to filter data by.
d. Read the input from the user (the integer).
e. Validate user input:
i. User input corresponds to one of the sought numbers [1, 7].
ii. If the user’s input is invalid, ask for re-entry.
iii. Allow the user to retry three times and then exit the program if the user keeps providing an invalid value.
4. Let’s ask the user to provide a floor value to filter our data by.
a. Read the input from the user.
b. Validate the user input:
i. User input is a positive integer.
ii. In the same way as 2eiii, allow three retries and then exit the program.
5. Now that we have the column name and a floor value to filter by, we need to filter the data by writing filter_data_by_column_and_floor (place the function in the same file as load_census_data and declare the new function right below it to keep your code organized).
a. the function must take a tuple (our data), a column name, and a floor value as parameters
b. iterate through each row of the data and build a new list of all the “rows” having the selected column’s value greater than the floor value.
c. Return a tuple built from the list created in b.
6. Invoke filter_data_by_column_and_floor with the appropriate arguments: the tuple of rows (our data), the column name selected by the user at step 3. (we want the string value here e.g. “Zip Code”.), the floor value selected by the user at step 5.
7. Now that we have data filtered let’s ask the user to know what column they want to sort by.
a. Read the input from the user (the integer).
b. Remember to validate the user input:
i. the input corresponds to one of the sought numbers[1, 7].
ii. Apply the same validation you did in 3.
8. Using the sorted function, sort the filtered data by the column name selected by the user. You may need to create a function as key to sort. (use the default sorting order).
9. Now, let’s save our data to a CSV file so the user can use it later. We are going to place the file into a folder named exports.
a. Create a folder named exports if it does not already exist. It is OK to create it wherever you want on the file system.
b. Prompt the user for a file name (make sure that is ends with “.csv” and is not empty).
c. Use the filename in conjunction with the “os.join” python function to create the path where the file will be saved under the folder exports
d. Make sure that the file does not already exist (ask for a new filename if it does)
i. Allow the user to retry three times and then exit the program if the user keeps providing an invalid filename.
e. Write the filtered, sorted data to the file.
i. Make sure to use with to open the file
ii. Use the csv module to write the file (
Code/Comment Format

Good code includes well named variables that are consistent from the beginning to the end of the program. Naming of objects should be self-explanatory. For instance, iterator_for_noun_list is much better than i.

Every program consists of a sequence of paragraphs, each of which has objectives, and which builds on the previous paragraphs. We are mostly interested in objectives that are valid at the end of the program so we can verify the program's design. The following is a preferred form for such paragraph headings. The # sign is adequate when the comment is a single line.

#This is an in-line comment – used to document the code for you, or anyone else, that intends
#To extend the code

In-line comments are helpful when one has to go back to the code 6 months later to make changes.

For doc strings, python allows the use of triple quotes. The triple quotes can be either single or double quotes. A doc sting is generally used as user documentation. It does not need to include details of the implementation of the program, but instead it provides documentation as how to use the API for the program (input, output etc.)

Solution PreviewSolution Preview

This material may consist of step-by-step explanations on how to solve a problem or examples of proper writing, including the use of citations, references, bibliographies, and formatting. This material is made available for the sole purpose of studying and learning - misuse is strictly forbidden.

import csv
import os
import sys

Function to load census data from provided filename
def load_census_data(filename):
    data = []
    with open(filename) as csvfile:
       reader = csv.reader(csvfile)
       for row in reader:
            if reader.line_num > 1 and len(row) == 7:
                   row = [int(x) for x in row]
    return tuple(data)

Function to filter data in specified column by floor value
def filter_data_by_column_and_floor(data, column, floor):
    index = data[0].index(column)
    filterData = list(data)
    for row in data[1:]:
       if row[index] < floor:
    return tuple(filterData)

# CSV File
FILE = "2010_Census_Populations_by_Zip_Code.csv"

# Load census data
data = load_census_data(FILE)
columns = data[0]
# Print menu
print("What data do you want to retrieve from the data set?")
i = 1
for column in columns:
    print("\t"+str(i)+") "+column)
    i += 1
# Query column to be retrieved
for i in range(3):
    choice = input("Enter choice: ")
    if choice in ['1','2','3','4','5','6','7']:
       choice = int(choice)
$65.00 for this solution

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

Find A Tutor

View available Python Programming Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Upload a file
Continue without uploading

We couldn't find that subject.
Please select the best match from the list below.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

  • 1
  • 2
  • 3
Live Chats