KnowledgeBoat Logo
|

Computer Science

Sonia was writing a code to insert multiple rows in the cust.csv file. By mistake her younger sister removed some parts of it. Now she is totally confused about used module and other options:

from csv import writer 
with ............... ("cust.csv", "a", newline = "\n") as f:     #line 1
    dt = writer(...............)                                #line 2
    while True:                                                #line 3 
        sno= int (input ("Enter Serial No: ") )
        cust_name = input ("Enter customer name: ")
        city = input ("Enter city: ")
        amt = int (input ("Enter amount: ") )
        dt. ............... ( [sno, cust name, city, amt] )          #line 4
        print ("Record has been added.")
        print ("Want to add more record?Type YES!!!")

        ch = input()                                                       #line 5
        ch = ch.upper()
        if ch=="YES": 
            print("*")
        else:
            break

Attempt any 4 out of the given questions:

(i) Name the method Sonia should use in Line 1.

(ii) Which object Sonia should use to connect the file in Line 2?

(iii) What is the use of Line 3?

(iv) Name the method she should use in Line 4.

(v) What is the use of Line 5?

Python File Handling

2 Likes

Answer

(i) Sonia should use the "open" method to connect to the "cust.csv" file in append mode.

(ii) Sonia should use the file object "f" to connect to the file.

(iii) Line 3 starts a while loop using while True:. This creates an infinite loop that allows Sonia to repeatedly input customer data and add records to the CSV file until she chooses to stop.

(iv) Sonia should use the "writerow" method to write a single row (list of values) to the CSV file.

(v) Line 5 reads the Sonia's input for whether she want to add more records to the CSV file.

Answered By

2 Likes


Related Questions