Question
struggling to remove the comma and space at the end of my output. please help. current output: {'a': '100', 'b': '200', 'c': '300' , }
struggling to remove the comma and space at the end of my output. please help.
current output:
{'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="") for k in record_dict: print(f"'{k}': '{record_dict[k]}'", 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)
\{'a': '100', 'b': '200', 'c': '300', \} \{'bananas': '1.85', 'steak': '19.99', 'cookies': '4.52', \} \{'a': '100', 'b': '200', 'c': '300'\} \{'bananas': '1.85', 'steak': '19.99', 'Cookies': '4.52'\}Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access with AI-Powered 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