0% found this document useful (0 votes)
68 views

File Handling - CSV File

Uploaded by

magevay740
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
68 views

File Handling - CSV File

Uploaded by

magevay740
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

File handling – CSV file

CSV (Comma Separated Values) is a file format for data storage which looks like a text
file. The information is organized with one record on each line and each field is separated by
comma.
▪ It is a common data exchange format used by the applications to produce and consume
data.
▪ A CSV file is a simple text file where each line contains a list of values (or fields) delimited
by commas (mostly), but you will encounter CSV files where data is delimited using tab (\t)
or pipe (|) or any other character.
▪ The first line of the CSV file represents the header containing a list of column names in the
file.
▪ CSV file is commonly used to represent tabular data
▪ Space-characters adjacent to commas are ignored
▪ Fields with in-built commas are separated by double quote characters
Consider the following example:
When to use CSV?
When data has a strict tabular structure
To transfer large database between programs
To import and export data to office applications
To store, manage and modify shopping cart catalogue

CSV Advantages
CSV is faster to handle.
CSV is smaller in size
CSV is easy to generate
CSV is human readable and easy to edit manually
CSV is simple to implement and parse
CSV is processed by almost all existing applications
CSV Disadvantages
No standard way to represent binary data
There is no distinction between text and numeric value.
Poor support of special characters and control characters.
CSV allows to move most basic data only. Complex configurations cannot be imported and exported
this way
Problem with importing CSV into SQL (no distinction between NULL and quotes)
Write / Read CSV FILE

Writing and reading operation from text file is very easy. First of all we have to import
csv module for file operation/method call.

Writing: We open file in ‘w’ writing mode using open() method which creates new file
object. Through csv.writer() method ,we create writer object to call writerow() method to
write objects.

Reading: We open the file in ‘r’ mode and create new file object, further we create
newfilereader object using csv.reader()method to read each row of the file.
Python Provides the following CSV module to work with csv file:
Main Functions are:
1. reader()
2. writer()
3. DictReader()
4. DictWriter()
1. reader() function : This function help us to read the csv file. This function takes
a file object and returns a csv.reader object that can be used to iterate over the
contents of a CSV file.
How to read entire data from data.csv file?
1 Marks
Q1. Which of the following module is provided by Python to do several operations on the CSV files?
(A)os (B) xls (C) csv (D) py

Q2. Which of the following tasks cannot be done or difficult with CSV module?
(A)Storing data in tabular form (B) Finding data uniquely (C) Saving data permanently (D) All of these

Q3. The writer() function of csv module has how many mandatory parameters?
(A)4 (B) 3 (C) 2 (D) 1

Q4. Which of these is not relevant to csv file?


(A) Text file (B) Binary file (C) Easily openable in text editor (D) None of the above
2 Marks
1. Assertion: To specify a different delimiter while writing into a csv file, delimiter
argument is used with csv.writer().
Reason: The CSV file can only take a comma as a delimiter.
(A) Both Assertion and reason are true and reason is the correct explanation of
assertion.
(B) Assertion and reason both are true but reason is not the correct explanation of
assertion.
(C) Assertion is true, reason is false.
(D) Assertion is false, reason is true.

2. Assertion: Every record in a CSV file is stored in reader object in the form of a list.
Reason: writerow() method allows us to write a list of fields to the CSV file.
(A) Both Assertion and reason are true and reason is correct explanation of assertion.
(B) Assertion and reason both are true but reason is not the correct explanation of
assertion.
(C) Assertion is true, reason is false.
(D) Assertion is false, reason is true.
3. Assertion: The CSV module in Python’s standard library presents methods to perform read/write
operations on CSV files.
Reasoning: To write data to a CSV file, we can use built-in function writer().
(A) Both Assertion and reason are true and reason is the correct explanation of assertion.
(B) Assertion and reason both are true but reason is not the correct explanation of assertion.
(C) Assertion is true, reason is false.
(D) Assertion is false, reason is true

4. Assertion: To open a CSV file employee.csv in write mode, we can give python statement as
follows:
fh=open('employee.csv’)
Reasoning: If we try to write on a csv file that does not exist, the file gets Created
(A) Both Assertion and reason are true and reason is the correct explanation of assertion.
(B) Assertion and reason both are true but reason is not the correct explanation of assertion.
(C) Assertion is true, reason is false.
(D) Assertion is false, reason is true.
5 Marks
Rohit, a student of class 12th, is learning CSV File Incomplete Code
Module in Python. During examination, he has been Import _______ #Statement-1
assigned an incomplete python code (shown below) fh = open( ___,_____ , newline='') #Statement-2
to create a CSV File 'Student.csv' (content shown stuwriter = csv. ______#Statement-3
below). Help him in completing the code which data = []
creates the desired CSV File. header = ['ROLL_NO', 'NAME', 'CLASS', 'SECTION']
CSV File data.append(header)
1,AKSHAY,XII,A for i in range(5):
2,ABHISHEK,XII,A roll_no = int(input("Enter Roll Number : "))
3,ARVIND,XII,A name = input("Enter Name : ")
4,RAVI,XII,A Class = input("Enter Class : ")
5,ASHISH,XII,A section = input("Enter Section : ")
rec = [ ]_____ #Statement-4
data.append(rec)
stuwriter. (data) _______#Statement-5
fh.close()
i. Identify the suitable code for blank space in line marked as Statement-1.
a) csv file b) CSV c) csv d) Csv

ii. Identify the missing code for blank space in line marked as Statement-2?
a) "School.csv","w" b) "Student.csv","w“ c) "Student.csv","r" d) "School.csv","r“

iii. Choose the function name (with argument) that should be used in the blank
space of line marked as Statement-3
a) reader(fh) b) reader(MyFile) c) writer(fh) d) writer(MyFile

iv. Identify the suitable code for blank space in line marked as Statement-4.
a) 'ROLL_NO', 'NAME', 'CLASS', 'SECTION' b) ROLL_NO, NAME, CLASS,
SECTION
c) 'roll_no','name','Class','section' d) roll_no,name,Class,section c) co.connect()

v. Choose the function name that should be used in the blank space of line marked
as Statement-5 to create the desired CSV File?
a) dump() b) load() c) writerows() d) writerow()
1 mark
Q1. (C) csv
Q2. (B) Finding data uniquly
Q3. (D) 1
Q4. (B) Binary file

2 marks
Q1. Assertion is true, reason is false.
Q2. (B) Assertion and reason both are true but reason is not the correct explanation of
assertion.
Q3. (C) Assertion is true, reason is false.
Q4. (D) Assertion is false, reason is true

5 Marks
Q1
i. c) csv
ii. b) "Student.csv","w"
iii.c) writer(fh)
iv.d) roll_no,name,Class,section
v. c) writerows()
Write a program to add/insert records in file “data.csv”. Structure of a record is roll number, name and
class.
Write a program to copy the data from “data.csv” to “temp.csv”
Write a program to read all content of “student.csv” and display records of only those students who scored more
than 80 marks. Records stored in students is in format: Rollno, Name, Marks
Write a program to calculate the sum of all the marks given in the file “marks.csv” . Records in “marks.csv” are as
follows:
Roll No Name Marks
1 Suman 67
2 Aman 71
3 Mini 68
4 Amit 80
Write a program to count the number of records present in “data.csv” file.
Write a program to show the details of the student who scored the highest marks. Data stored in “Data.csv” is
given below:
Roll No Name Marks
1 Suman 67
2 Aman 71
3 Mini 68
4 Amit 80

You might also like