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

CSV File Handling in Python Guide

The document provides a comprehensive overview of CSV (Comma Separated Values) file handling in Python, including definitions, characteristics, advantages, and disadvantages of CSV files. It also covers the usage of the csv module for reading and writing CSV files, along with examples of functions like writerow() and reader(). Additionally, it includes practical programming tasks related to CSV file operations.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views4 pages

CSV File Handling in Python Guide

The document provides a comprehensive overview of CSV (Comma Separated Values) file handling in Python, including definitions, characteristics, advantages, and disadvantages of CSV files. It also covers the usage of the csv module for reading and writing CSV files, along with examples of functions like writerow() and reader(). Additionally, it includes practical programming tasks related to CSV file operations.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Questions on CSV File Handling in Python

Q. 1 What is full form of CSV?


Ans. CSV (Comma Separated Values)
Q. 2 What is CSV file? What are its characteristics?
Ans.
 CSV (Comma Separated Values) is a file format for data storage which looks like a
text file.
 It is used to store tabular data, such as a spreadsheet or database.
 The information is organized with one record on each line
 Each field is separated by comma.
 You can choose a different delimiter character such as ‘ ‘:’ , ‘;’ , ‘|’.
 In CSV files space-characters adjacent to commas are ignored
Q.3 When do we use CSV file?
Ans.
 When data has a strict tabular structure
 To transfer large data between programs
 To import and export data
Q. 4 What are the Advantages of CSV files?
Ans. Advantages of CSV:
 CSV is faster to handle
 CSV is smaller in size and 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
Q. 5 What are the Disadvantages of CSV files?
Ans. Disadvantages of CSV:
 There is no standard way to represent binary data
 There is no distinction between text and numeric values
 There is 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
 There are problems with importing CSV into SQL (no distinction between NULL and
quotes)
Q. 6 Which module is used to operate on CSV file?
Ans. To read and write in CSV files we need to import csv module.
Q. 7 CSV files are opened with __________argument to supress EOL translation.
Ans. newline=’ ‘
Q.8 What does csv writer() function do?
Ans.
 The [Link]() function returns a writer object that converts the user’s data into a
delimited string.
 This writer object can be used to write into CSV files using the writerow() function.
 Syntax
 writerobj = [Link](filehandle, delimiter=’ ‘)
Where filehandle = filehandle returned from open() function

delimiter = delimiter to be used to separate columns

e.g.
import csv
f1=open(‘[Link]’,’w’)
w1=[Link](f1,delimiter = ‘,’)
Q. 9 What is the difference between writer object’s writerow() and writerows()
function?
Ans. [Link](row): Write the row parameter to the writer’s file object, formatted
according to delimiter defined in writer function
e.g.
import csv
f1=open(‘[Link]’,’w’)
w1=[Link](f1,delimiter = ‘,’)
[Link]([‘[Link]’, ‘Name’, ‘Marks’])
[Link]()writer.
writerows(rows): Writes multiple rows (sequence) to the writer’s file object
e.g.
import csv
f1=open(‘[Link]’,’w’)
w1=[Link](f1,delimiter = ‘,’)
[Link]([‘[Link]’, ‘Name’, ‘Marks’])
[Link]([[1, ‘Ronit’, 84],[2,’Nihir’,90]])
[Link]()
Q.10 What is csv reader() function?
Ans.
 The [Link] () function returns a reader object that helps in reading rows as per
the delimiter specified.
 This reader object is used to iterate over lines in the given csvfile.
 Each row read from the csv file is returned as a list of strings.
Syntax
readerobj = [Link](filehandle, delimiter=’,‘)
Where filehandle = filehandle returned from open() function
delimiter = delimiter to be used to separate columns
e.g.
import csv
f1=open(‘[Link]’,‘r’)
w1=[Link](f1,delimiter = ‘,’)

Q.11 Which of the following file types can be opened with notepad as well as ms
excel?
1. Text Files
2. Binary Files
3. CSV Files
4. None of these
Ans. c
Q. 12 What is with statement in Python?
Ans. with statement in Python is used in to simplify the management of common
resources like file streams. It make the code cleaner and much more readable. For e.g.
there is no need to call [Link]() when using with statement.
e.g.

with open(‘file_path’, ‘w’) as file:


[Link](‘hello world !’)
Q. 13 What does tell() method do?
Ans. It tells you the current position of cursor within the file
Syntax: file_object.tell()
Q. 14 What does seek() method do?
Ans. Seek() method can be used to changes the current file position
Syntax:
[Link](offset[, from])
Offset: number of bytes to be moved.
From: 0 – Beginning of the file
1 – Current Position
2 – End of the file
Q. 15 What is difference between tell() and seek() methods
Ans.
tell() seek()
It returns the current position of cursor in file. Change the cursor position by bytes as specified by
Example: Example:
fout=open(“[Link]”,”w”) fout=open(“[Link]”,”w”)
[Link](“Welcome Python”) [Link](“Welcome Python”)
print([Link]( )) [Link](5)
[Link]( ) print([Link]( ))
[Link]( )
Output: Output:
4 5
Q. 16 Write a program to write into file “[Link]” Rollno, Name and Marks
separated by comma. It should have header row and then take in input from the
user for all following rows. The format of the file should be as shown if user enters
2 records.
[Link],Name,Marks
20,ronit,67
56,nihir,69
Ans.
import csv
f1=open('[Link]','w',newline=‘ ')
w1=[Link](f1,delimiter = ",")
[Link](['[Link]', 'Name', 'Marks'])
while True:
print ("Enter 1 to continue adding, 0 to exit")
op = int(input("Enter Option"))
if (op == 1):
rollno = int(input("Enter Roll No"))
name = input("Enter Name")
marks = int(input("Enter Marks"))
wlist = [rollno,name,marks]
[Link](wlist)
elif op == 0:
break;
[Link]()

Q. 17 Write a program to read all content of “[Link]” and display records of


only those students who scored more than 80 marks. Records stored in students
is in format : Rollno, Name, Marks
Ans.
import csv
f=open("[Link]","r")
d=[Link](f)
next(f)
print("Students Scored More than 80")
print()
for i in d:
if int(i[2])>80:
print("Roll Number =", i[0])
print("Name =", i[1])
print("Marks=", i[2])
[Link]( )

Q.18 Write a program to count number of records present in “[Link]” file.


Ans.
import csv
f = open("[Link]" , "r")
d = [Link](f)
next(f) #to skip header row
r = 0
for row in d:
r = r+1
print("Number of records are " , r)
Q.19 What is the output of the following program if the [Link] file contains
following data?
[Link]
Ronit, 200
Akshaj, 400
Program
import csv
d = [Link](“[Link]”)
next (d)
for row in d:
print (row);

Ans. Akshaj, 400


Q. 20 Write a program to copy the data from “[Link]” to “[Link]”
Ans.
import csv
f=open("[Link]","r")
f1=open("[Link]",'w')
d=[Link](f)
d1=[Link](f1)
for i in d:
[Link](i)
[Link]( )
[Link]( )

Q.21 Ronit has a CSV file “[Link]” which has name, class and marks
separated by comma. He is writing a Python program to copy only the name and
class to another CSV file “[Link]”. He has written the following code. As a
programmer, help him to successfully execute the given task.
import csv
file = open('[Link]', a , newline="");
writer = [Link](file)

b open('[Link]') as csvfile:
data = csv. c (csvfile)
for row in data:
[Link]([ d , e ])

[Link]()

1. In which mode should Ronit open the file to make a new file?
2. Which Python keyword should be used to manage the file stream?
3. Fill in the blank in Line 3 to read the data from a csv file.
4. Fill in the blank to write name into [Link]
5. Fill in the blank to write class into [Link].
Ans.
import csv
file = open('[Link]', 'w', newline="");
writer = [Link](file)

with open('[Link]') as csvfile:


data = [Link](csvfile)
for row in data:
[Link]([row[0],row[1]])

[Link]()

You might also like