0% found this document useful (0 votes)
7 views11 pages

Understanding CSV Files and Python Usage

The document provides an overview of CSV (Comma Separated Values) files, detailing their structure, advantages, and usage in Python through the csv module. It outlines how to read from and write to CSV files, the significance of delimiters, and includes examples of user-defined functions for managing CSV data. Additionally, it compares CSV files with text and binary files, highlighting their human-readable format and ease of use for data storage.

Uploaded by

biraroj836
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views11 pages

Understanding CSV Files and Python Usage

The document provides an overview of CSV (Comma Separated Values) files, detailing their structure, advantages, and usage in Python through the csv module. It outlines how to read from and write to CSV files, the significance of delimiters, and includes examples of user-defined functions for managing CSV data. Additionally, it compares CSV files with text and binary files, highlighting their human-readable format and ease of use for data storage.

Uploaded by

biraroj836
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

CSV FILES

 CSV stands for Comma Separated Values.


 CSV is just like a text file, in a human readable format which is extensively
used to store tabular data, in a spreadsheet or database.
 The separator character of CSV files is called a delimiter.
 Default delimiter is comma(,). Other delimiters are tab(‘\t’), colon(:),
pipe(|) and semi colon(;) characters.
wo=[Link](f, delimiter=’;’)
 It is used for storing tabular data in a spreadsheet or database.
 Each record consists of fields separated by Commas(delimiter)
 Each line of a file is called a record.

Advantages of CSV Files:


 Easier to create.
 Preferred import and export format for databases and spreadsheets.
 Capable of storing large amount of data.

Python csv Module


csv module provides two type of objects.
reader – to read from the csv files.
writer – to write in to the csv files.
To import csv module in our program, write the following statement:
import csv

Opening/Closing csv files


Open a csv file :
f=open(“[Link]”,”w”)
OR
f=open(“[Link]”, “r”)
Close a csv file:
[Link]()

Role of Argument newline in Opening of csv files


 Newline argument specifies how would Python handle new line
characters while working with csv files, on different Operating systems.
 Different operating systems store EOL characters differently.

Writing in csv files


[Link]() - Returns a writer object which writes data into csv
files.
<WriterObject>.writerow() - Writes one row of data on to the writer
object.
<WriterObject>.writerows() - Writes multiple rows of data on to the
writer object. We use Nested list to implement this.
Q1. Write one difference between CSV and text files.

Ans:-

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

Q2. Give any one point of difference between a binary file and a CSV file.

Ans:- Difference between a binary file and a CSV file:-

Binary file

 Extension is .dat
 Not human readable
 Stores data in the form of 0s and 1s

CSV file

 Extension is .csv
 Human readable
 Stores data like a text file

Q3. Assertion(A): CSV file is a human readable text file where each line has a number of fields,
separated by commaor some other delimiter.

Reason(R): writerow() method is used to write a single row in a CSV file.

(a) Both (A) and (R) are true and (R) is the correct explanation for (A).
(b) Both (A) and (R) are true and (R) is not the correct explanation for (A).
(c) (A) is true but (R) is false.
(d) (A) is false but (R) is true.

Q4. Which of the following character acts as default delimiter in a CSV file?

a) (colon):
b) (hyphen)-
c) (comma),
d) (vertical line) |

Q5. Syntax for opening [Link] file in write mode is

myfile=open("[Link]","w",newline=").

What is the importance of newline=”?

a) A newline gets added to the file


b) Empty string gets appended to the first line.
c) Empty string gets appended to all lines.
d) EOL translation is suppressed

Q6. Which of the following is not a function/method of csv module in Python?

a) read()

b) reader()

c) writer()

d) writerow()

Q7. What is the advantage of using a CSV file for permanent storage ?

Ans:- Advantage of a CSV file:-

It is human readable- can be opened in Excel and Notepad applications.

It is just like text file.

Q8. A CSV file “[Link]” contains the data of a survey. Each record of the file contains the
following data:

Name of a country

Population of the country

Sample size

Number of persons who accepted that they were Happy

For example, a sample record of the file may be: [‘Signiland’,5673000, 5000, 3426]

Write the following Python functions to perform the specified operation on this file:

i) Read all the data from the file in the form of a list and display all those records for which
the population is more than 5000000.
ii) Count the number of records in the file.
Q9:- Mr. Mahesh is a Python Programmer working in a school. He has to maintain the records of the
records of the sports students. He has created a csv file named [Link], to store the details. The
structure of [Link] is:

[sport_id, competition, prize_won]

Where

sport_id, is Sport id(integer)

competition is competition name(string)

prize_won is(“Gold”, “Silver”, “Bronze”)

Mr. Mahesh wants to write the following user-defined functions:

Add_detail(): to accept the detail of a student and add to csv file “[Link]”.

Count_Medal(): to display the name of competitions in which students have won “Gold” medal.

Help him in writing the code of both the functions.


Q10. 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 [Link], to store
the details.

The structure of [Link] is:

[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, [Link]

Count_Device() : To count and display number of peripheral devices whose price is less than 1000.
Q11. Vedansh is a Python programmer working in a school. For the Annual Sports Event, he has
created a csv file named [Link], to store the results of students in different sports events. The
structure of [Link] 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 add it to the file [Link], 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.


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

(i) Add_Device(): The function accepts and adds records of the peripheral devices to a csv file
'[Link]'.

Each record consists of a list with field elements as P_id, P_name and Price to store peripheral device
ID, device name, and price respectively.

(ii) Count_Device(): To count and display number of peripheral devices, whose price is less than
Rs.1000 .
Q13. Write a program in Python that defines and calls the following user defined functions:

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

(ii) COURIER_SEARCH(): Takes the destination as the input and displays all the courier records going to
that destination.

You might also like