Data File Handling
Data File Handling
Data File Handling
Text File
A text file is a computer file that only contains
text and has no special formatting such as bold
text, italic text, images, etc. With Microsoft
Windows computers text files are identified
with the .txt file extension
When you
open a text
file in
notepad
When you
open a
binary file
in notepad
When you
open a csv
file in
notepad
CBSE Sample paper (1 mark)
Assertion (A): CSV (Comma Separated Values) is a
file format for data storage which looks like a text
file.
Reason (R): The information is organized with one
record on each line and each field is separated by
comma.
Ans: (a) Both A and R are true and R is the correct explanation for A
CBSE Sample paper (1 mark)
What is the advantage of using a csv file for
permanent storage?
import csv
Reading a csv file in Python
Second, open the CSV file using the built-in open() function
in the read mode:
f = open('path/to/csv_file')
Reading a csv file in Python
Third, pass the file object (f) to the reader() function of the
csv module. The reader() function returns a csv reader
object:
csv_reader = csv.reader(f)
Printing data of csv file in Python
The csv_reader is an iterable object of lines from the CSV file. Therefore,
you can iterate over the lines of the CSV file using a for loop:
Each line is a list of values. To access each value, you use the square bracket
notation []. The first value has an index of 0. The second value has an index of 1, and
so on.
For example, the following accesses the first value of a particular line:
line[0]
Closing a file in Python
Finally, always close the file once you’re no
longer access it by calling the close() method
of the file object:
f.close()
Suppose a file named as ‘students.csv’ is exists in current folder. Each line
contains three values separated by comma. First represent the roll number,
second as student name and third as marks obtained.
Create a function ‘show’ to read csv file and show its contents on the
screen.
CODING OUTPUT
Suppose a file named as ‘students.csv’ is exists in current folder. Each line
contains three values separated by comma. First represent the roll number,
second as student name and third as marks obtained.
Write python code to read csv file and show name and marks of each
student.
CODING OUTPUT
Suppose a file named as ‘students.csv’ is exists in current folder. Each line
contains three values separated by comma. First represent the roll number,
second as student name and third as marks obtained.
Write a python function toppers() read csv file and show roll number and
marks of each student where marks is more than or equal to 90.
CODING OUTPUT
Consider a csv file ‘cricket.csv’ stored in a computer which contain three values
in a line representing playerid, player name and runs.
Define a function ‘report’ to
i) Show player name and runs of all those players where runs are more than
10.
ii) Count and display total number of players where runs are more than 10.
CODING OUTPUT
Writing CSV Files in Python
To write data into a CSV file, you follow these steps:
• First, open the CSV file for writing (w mode) by using the open()
function.
• Second, create a CSV writer object by calling the writer() function of
the csv module.
• Third, write data to CSV file by calling the writerow() or writerows()
method of the CSV writer object.
• Finally, close the file once you complete writing data to it.
Write a program to save the following data in a csv file
‘capitals.csv’
f = open('capitals.csv','w', newline='')
Writing multiple rows to CSV files
To write multiple rows to a CSV file at once, you use the
writerows() method of the CSV writer object.
empid,ename,salary
111,Harish,5000
222,Sunita,7000
333,Anita,4500
444,Sita,10000