CSV File Handling Board Questions


  1. Sangeeta is a Python programmer working in a computer hardware company. She has to maintain the records of the peripheral devices. She created a csv file named Peripheral.csv, to store the details. The structure of Peripheral.csv is : [4 MARKS] [2023]
    [P_id, P_name, Price]

    Where
    P_id is Peripheral device ID (integer)
    P_name is Peripheral device name (String)
    Price is Peripheral device price (integer)

    Sangeeta wants to write the following user defined functions :
    Add_Device() : to accept a record from the user and add it to a csv file, Peripheral.csv .
    Count_Device() : to count and display the number of peripheral devices whose price is less than 1000.
  2. coming soon

  3. Why is it important to close a file before exiting? [5 MARKS] [2023]

    Write a program in Python that defines and calls the following user defined functions :

    (a) ADD_BOOK( ) : Takes the details of the books and adds them to a csv file ‘Book.csv’. Each record consists of a list with field elements as book_ID, B_name and pub to store book ID, book name and publisher respectively.

    (b) SEARCH_BOOK( ) : Takes publisher name as input and counts and displays number of books published by them.
  4. It is important to close the file before exiting as Python makes sure that any 
    unwritten or unsaved data is flushed off to the file before it is closed.
    
    
    import csv 
    def Add_Book():
           f1=open("Book.csv","a",newline="\n")
           writ=csv.writer(f1)
           book_ID=int(input("Enter the Book id"))
           B_name=input("Enter the Book Name")
           pub=input("Enter the Publisher Name")
           detail=[book_ID, B_name,pub]
           writ.writerow(detail)
           f1.close()
    
    def Search_Book ():
           f1=open("Book.csv","r") # ignore newline
           detail=csv.reader(f1)
           name=input("Enter the Publisher Name to be searched")
           pub_count=0
           for i in detail :
                 if i[2]==name:
                       pub_count+=1
           print("NUMBER OF BOOKS: ",pub_count)
    
    Add_Book()
    Search_Book()
    

  5. Write one difference between CSV and text files. [5 MARKS] [2023]

    Write a program in Python that defines and calls the following user defined functions :

    (a) COURIER_ADD( ) : It takes the values from the user and adds the details to a csv file ‘courier.csv’. Each record consists of a list with field elements as cid, s_name, Source, destination to store Courier ID, Sender name, Source and destination address respectively.

    (b) COURIER_SEARCH( ) : Takes the destination as the input and displays all the courier records going to that destination.
  6. CSV files
    ● can be viewed in spreadsheets
    ● module CSV has to be imported
    Text files
    ● can be viewed in the text editor
    ● No specific module required to be imported
    
    
    import csv
    def COURIER_ADD() :
           f1=open("courier.csv","a",newline="\n")
           writ=csv.writer(f1)
           cid=int(input("Enter the Courier id"))
           s_name=input ("Enter the Sender Name")
           Source=input("Enter the Source Address")
           destination=input("Enter Destination Name")
           detail=[cid,s_name,Source,destination]
           writ.writerow (detail)
           f1.close()
    
    def COURIER_SEARCH() :
           f1=open("courier.csv","r") # ignore newline
           detail=csv.reader(f1)
           name=input("Enter the Destination Name to be searched")
           for i in detail :
                 if i[3]==name:
                       print("Details of courier are: ",i)
    
    COURIER_ADD()
    COURIER_SEARCH()