Question
struggling to make my code output look like this. {'a': '100', 'b': '200', 'c': '300'} {'bananas': '1.85', 'steak': '19.99', 'cookies': '4.52' current output is: {a:100,b:200,c:300}
struggling to make my code output look like this.
{'a': '100', 'b': '200', 'c': '300'}
{'bananas': '1.85', 'steak': '19.99', 'cookies': '4.52'
current output is:
{a:100,b:200,c:300}
{bananas:1.85,steak:19.99,cookies:4.52}
Current code:
# import the csv module import csv
# method to print a given dictionary to match the sample output def printDict(record_dict): print("{", end = "") # loop through the given dictionary for i in range(len(record_dict)): # get the current key k = list(record_dict)[i] # print the current key and its corresponding value print(f"{k}:{record_dict[k]}", end = "") # print a comma if it is not the last record if i < len(record_dict) - 1: print(",", end = "") print("}")
# create 2 dictionaries to store the rows from the input file dict1, dict2 = {}, {} # variable to keep track of the record number n = 1
# input the csv file's name to be parsed filename = input() # open the file in read mode inFile = open(filename, 'r')
# read each record from the file using the reader() function of csv module for record in csv.reader(inFile): # loop through each pair of tokens of the current record for i in range(0, len(record), 2): # if it is the first record if n == 1: # save the current & the next token as key-value pairs in dict1 dict1[record[i].strip()] = record[i + 1].strip() # otherwise it is the second record elif n == 2: # save the current & the next token as key-value pairs in dict2 dict2[record[i].strip()] = record[i + 1].strip() # increment the record number n += 1 # close the input file inFile.close()
# print both the dictionaries printDict(dict1) printDict(dict2)
please help!
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