Question: # create two dictionaries to store the rows from the input file dict 1 , dict 2 = { } , { } # variable

# create two 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 the csv module
for record in csv.reader(inFile):
# loop through each pair of tokens in the current record
for i in range(0, len(record),2):
# if it is the first record
if n ==1:
# save the current and 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 and 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)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!