Exercise - Processing CSV files using a dictionary of lists

I couldn’t find anything on the exercise for lecture 3 for processing csv files using a dictionary of lists. I found it pretty difficult but finally cracked it so thought I’d post my comment in case anyone else is trying this and needs some hints:

# Step 1 - Download files
# Import OS and urllib request module to be used later
# Create folder called data2 in directory
# retrieve files from url, and add them to data2 folder with names
# Step 2 - Create function to read data and return dictionary
# Take a csv file as input
# Open the file and convert the data into a list of lines
# Take the first line and format it to create a list of headers
# Take each column of data and put it into a list
# First create an empty list for the columns
# Next iterate through the columns based on the number of headers
# Create an empty list for each column of data
# Now iterate through each line / row of data (excluding the header line)
# Format the line of data
# Check the line for missing items and convert them to 0.0
# Add the data point from the row and respective column to the column list
# Add the full list of column of data to the columns list
# Create an empty dictionary
# Pair up each column of data to the respective headers
# Add the key (header) and value (column of data) to the dictionary
# Return the dictionary as output
# Step 3 - Create function to compute EMIs
# Take a dictionary (e.g. from Step 2) as an input
# import math module as required later for rounding
# Create an empty list
# Iterate through each loan
# Assign variables for each item in the loan
# Calculate emi
# Round emi
# append each emi to the list of emis
# Assign the list of the emis to the key and add to the dictionary
# Return the updated dictionary with the emis added
# Step 4 - Create a function to write a csv file with the loans and EMIs
# Take the dictionary as an input, and define the output file location and name
# Create a new file using the input provided
# Collect the headers from the keys
# Join the headers together into csv format and write to the file
# Iterate through the loans based on the total number of loans
# Create a list for each loan and covert the floats back into strings
# Join the values together into csv format and write to the file
1 Like

Nice one, i would really like to see how you went about step 4, i’m stuck there

# Step 4 - Create a function to write a csv file with the loans and EMIs

# Take the dictionary as an input, and define the output file location and name
def write_csv_columnar(input_dict, output_file):

    # Create a new file using the input provided
    with open(output_file, 'w') as f:

        # Collect the headers from the keys
        headers = list(input_dict.keys())
        # Join the headers together into csv format and write to the file
        f.write(','.join(headers) + "\n")

        # Iterate through the loans based on the total number of loans
        for i in range(len(input_dict['amount'])):
            # Create a list for each loan and covert the floats back into strings
            values = [
                str(int(input_dict['amount'][i])),
                str(int(input_dict['duration'][i])),
                str(input_dict['rate'][i]),
                str(int(input_dict['down_payment'][i]))
            ]
            # Join the values together into csv format and write to the file
            f.write(','.join(values) + "\n")
1 Like

I would really appreciate it if you send the code here. I have been stuck in this for 3 days now. Still cannot get my way around it.
Thanks

Hey, You can check this post,
The following post is of @jagarwalj1998, Thanks to him for sharing his version of the code.

1 Like

Which part of the problem are you stuck on? I have laid out all the comments above so let me know which line(s) you’re stuck on then I can help.

Joe

1 Like