Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

# 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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Databases Demystified

Authors: Andrew Oppel

1st Edition

0072253649, 9780072253641

More Books

Students also viewed these Databases questions