Data File Handling Working With CSV Files
Data File Handling Working With CSV Files
Data File Handling Working With CSV Files
Chapter-8
: File
File Handling
Handling -> CSV File
In this tutorial we will discuss the following topics
S.No. Topics
Page 1 of 9
Unit I: Computational Thinking and Programming - 2
Chapter-8
: File
Python: Handling
File Handling -> CSV Files
What is CSV file?
In this type of file, there is no terminator for a
line and the data is stored after converting it into
machine understandable binary language.
Operations on binary files are faster as no
translation required.
Image files such as .jpg, .png, .gif, etc., and documents such as .doc, .xls, .pdf,
etc., all of them constitute binary files.
Read mode (denoted by ‘r’): It reads contents from a file. The file should exist else it
gives an error.
Write mode (denoted by ‘w’): It writes to a CSV file. If the file exists, it clears it and
starts writing to it from the first row. Else, if the file does not exist, it is created and
then the data is written to it.
Chapter-8
: File
e) Searching Record in CSVfile Handling
f) Deleting record from CSVfile
g) Modification/ Updation in CSV File.
import csv
Page 3 of 9
Unit I: Computational Thinking and Programming - 2
Chapter-8
: File
Writing into CSV using loop: Handling
import csv
writecsv=csv.writer(csvf,delimiter=',')
choice='y'
while choice.lower()=='y':
n=input("Enter Name")
p=float(input("Enter Percentage"))
r=input("Enter Remarks")
writecsv.writerow([rl,n,p,r])
The csv.reader() method returns a reader object which iterates over lines in the
given CSV file.
Step 1: You have to imports the csv package so that you can use the methods
provided by it for easy csv I/O.
Step 2: opens the file student_Details in read mode and a file object called
fileobject, which points to the file contents.
Step 3: csv.reader is the object which iterates over the lines of the CSV file through
the File object readcsv.
Page 4 of 9
Unit I: Computational Thinking and Programming - 2
Chapter-8
Example: : File Handling
import csv
readcsv=csv.reader(fileobject)
for i in readcsv:
print(i)
Example: We will show how to pull out specific data from the spreadsheet and save
it to a list variable:
import csv
with open('Student_Details.csv') as csvf:
readCSV = csv.reader(csvf, delimiter=',')
RollNos = []
Remarks = []
for row in readCSV:
Rem = row[3]
Roll = row[0]
RollNos.append(Rem)
Remarks.append(Roll)
print(RollNos)
print(Remarks)
Page 5 of 9
Unit I: Computational Thinking and Programming - 2
Chapter-8
Appending to CSV files : File Handling
Suppose we want to add a few lines to the “Student_Details.csv” file. The Append
mode ‘a’ can be of help. The below code appends rows to the existing
“Student_Details.csv” file:
import csv
writecsv=csv.writer(csvfile,delimiter=',')
choice='y'
while choice.lower()=='y':
n=input("Enter Name")
p=float(input("Enter Percentage"))
r=input("Enter Remarks")
writecsv.writerow([rl,n,p,r])
Page 6 of 9
Unit I: Computational Thinking and Programming - 2
Chapter-8
Copy data from one file:toFile
anotherHandling
import csv
fileWriter = csv.writer(outFile)
fileReader = csv.reader(inFile)
fileWriter.writerow(row)
In the above code, first we open the file (in write mode) to which we want to copy
the contents, i.e. destination file (copyout.csv). Then, we create the writer object.
Further, we open the file (in read mode) from which we need to copy the contents
from, i.e. the source file (Student_Details.csv).
Page 7 of 9
Unit I: Computational Thinking and Programming - 2
Chapter-8
Searching Record in CSV: file
File Handling
(Search By Roll number)
import csv
with open('Student_Details.csv') as f:
#if current rows index value (here 0) is equal to input, print that row
if number ==row[0]:
print (row)
OUTPUT:
Enter number to find: 103
Page 8 of 9
Unit I: Computational Thinking and Programming - 2
Chapter-8
: File
Deleting record from CSV Handling
file by field value
For deleting the particular row from CSV file.
Example:
import csv
lines = list()
reader = csv.reader(readFile)
lines.append(row)
if field == rno:
lines.remove(row)
writer.writerows(lines)
readf = csv.reader(rFile)
for i in readf:
print(i)
Page 9 of 9