0% found this document useful (0 votes)
38 views29 pages

Data File Handling

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 29

Three Types of Files

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?

Advantage of a csv file:


• It is human readable – can be opened in Excel
and Notepad applications
• It is just like text file
What is a CSV file
• CSV stands for comma-separated values. A CSV file is a delimited text file that uses a comma to separate
values.
• A CSV file consists of one or more lines. Each line is a data record. And each data record consists of one
or more values separated by commas.
• Typically, you use a CSV file to store tabular data in plain text. The CSV file format is quite popular and
supported by many software applications such as Microsoft Excel and Google Spreadsheet.
Reading a csv file in Python

To read a CSV file in Python,


First, import the csv module:

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:

for line in csv_reader:


print(line)

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’

Assam, Dispur, Assamese


Bihar, Patna, Hindi
Chhattisgarh, Raipur, Chhattisgarhi
Goa, Panaji, Konkani
Gujarat, Gandhinagar, Gujarati
Haryana, Chandigarh, Haryanvi
If you open the capitals.csv,
you’ll see one issue that the file
contents have an additional
blank line between two
subsequent rows :

To remove the blank line, you


pass the keyword argument
newline='' to the open() function
as follows:

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.

The writerows() method is used to write multiple rows at a


time i.e., it can be used to write the contents of a 2-
dimensional list into a csv file. Row lists can be written using
this method.
Write a program in Python to write the following data in a
file ‘employee.csv’ using writerows() method.

empid,ename,salary
111,Harish,5000
222,Sunita,7000
333,Anita,4500
444,Sita,10000

You might also like