Page 150 - PYTHON-12
P. 150

6.  Write a statement in Python to perform the following operations:
                    (a)  To open a text file "Book.txt" in read mode
                    (b)  To open a binary file "Book.dat" in write mode
               Ans.  (a)  f = open("Book.txt", "r")
                    (b)  f = open("Book.dat", "wb")
                7.  What is a CSV file?
               Ans.  CSV (Comma Separated Values) is a simple file format used to store tabular data, such as a spreadsheet
                   or database. A CSV file stores tabular data (numbers and text) in plain text.
                8.  What are the advantages of CSV file formats?
               Ans.  Advantages:
                    (a)  A simple, compact and ubiquitous format for data storage.
                    (b)  A common format for data interchange.
                    (c)  It can be opened in popular spreadsheet packages like MS-Excel, Cal, etc.
                    (d)  Nearly all spreadsheets and databases support import/export to csv format.
                9.  Differentiate between a text file and a binary file.
               Ans.  A text file stores data as ASCII/UNICODE characters whereas a binary file stores data in binary format
                   (as it is stored in memory). Internal conversion is required in text file and, hence, slower but binary file
                   does not need any translation and is faster.

                10.  Write a program to add (append) Employee records onto a csv file.
               Ans.  import csv
                   with open('myfile.csv',mode = 'a') as csvfile:
                       mywriter = csv.writer(csvfile, delimiter = ',')
                       ans = 'y'
                       while ans.lower() == 'y':
                           eno = int(input("Enter Employee Number:"))
                           name = input("Enter Employee Name:")
                           salary = int(input("Enter Employee Salary:"))
                           mywriter.writerow([eno,name,salary])
                           print("## Data Saved… ##")
                           ans = input("Add More?")
                11.  Write user-defined functions to perform read and write operation onto a student.csv file having fields as
                   roll number, name, stream and marks.

               Ans.  import csv
                   row = ['2','Akshat Chauhan','Commerce','98']
                   def readcsv():
                       with open("D:/student.dat", 'r') as f:
                           data = csv.reader(f)
                           # reader function to generate a reader object
                           for row in data:
                               print(row)
                   def writecsv():
                       with open("D:/student.dat", 'w', newline='') as fobj:
                           # write new record in file
                           csv_w = csv.writer(fobj, delimiter=',')
                           csv_w.writerow(row)
                   print("Press-1 to Read Data and Press-2 to Write data: ")
                   a = int(input())
                   if a == 1:
                       readcsv()
                   elif a == 2:
                       writecsv()                                                                                 Python Libraries
                   else:
                       print("Invalid value")
                                                                                                            4.21
   145   146   147   148   149   150   151   152   153   154   155