English

What is the advantage of using a csv file for permanent storage? Write a Program in Python that defines and calls the following user defined functions: - Computer Science (Python)

Advertisements
Advertisements

Question

What is the advantage of using a CSV file for permanent storage? Write a Program in Python that defines and calls the following user-defined functions:

  1. ADD() - To accept and add data of an employee to a CSV file ‘record.csv’. Each record consists of a list with field elements such as empid, name, and mobile to store an employee id, employee name, and employee salary respectively.
  2. COUNTR() - To count the number of records present in the CSV file named ‘record.csv’.
Answer in Brief

Solution

Advantages of a CSV file:

  • It is human readable - can be opened in Excel and Notepad applications.
  • It is just like a text file.
Program:

import csv
def ADD():
fout=open("record.csv","a",newline="\n")
wr=csv.writer(fout)
empid=int(input("Enter Employee id :: "))
name=input("Enter name :: ")
mobile=int(input("Enter mobile number :: "))
lst=[empid,name,mobile] ---------1/2 mark
wr.writerow(lst) ---------1/2 mark
fout.close()

def COUNTR():
fin=open("record.csv","r",newline="\n")
data=csv.reader(fin)
d=list(data)
print(len(d))
fin.close()

ADD()
COUNTR()
shaalaa.com
Read and Write a CSV File Using Python
  Is there an error in this question or solution?
2022-2023 (March) Sample

RELATED QUESTIONS

Mention the two ways to read a CSV file using Python.


Mention the default modes of the File.


Write a Python program to read a CSV file with default delimiter comma (,).


What is the difference between the write mode and append mode?


What is the difference between reader() and DictReader() function?


Write the different methods to read a File in Python.


Write a Python program to write a CSV File with custom quotes.


Write the rules to be followed to format the data in a CSV file.


Give any one point of difference between a binary file and a CSV file. Write a Program in Python that defines and calls the following user-defined functions:

  1. add() - To accept and add data of an employee to a CSV file ‘furdata.csv’. Each record consists of a list with field elements such as fid, fname, and fprice to store furniture id, furniture name, and furniture price respectively.
  2. search() - To display the records of the furniture whose price is more than 10000.

Select the correct output of the code:

S="Amrit Mahotsav @ 75"
A=S.partition (" ")
print(a)

Vedansh is a Python programmer working in a school. For the Annual Sports Event, he has created a csv file named Result.csv, to store the results of students in different sports events. The structure of

Result.csv is:

[St_Id, St_Name, Game_Name, Result]

Where

St_Id is Student ID (integer)

ST_name is Student Name (string)

Game_Name is name of game in which student is participating(string). Result is result of the game whose value can be either 'Won', 'Lost' or 'Tie'.

For efficiently maintaining data of the event, Vedansh wants to write the following user defined functions:

Accept() - to accept a record from the user and add it to the file

Result.csv. The column headings should also be added on top of the csv file.

wonCount() - to count the number of students who have won any event.

As a Python expert, help him complete the task.


Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×