KEMBAR78
CH 5 - CSV Files | PDF | Comma Separated Values | Data
0% found this document useful (0 votes)
36 views24 pages

CH 5 - CSV Files

This document provides an overview of working with CSV (Comma-Separated Values) files in Python, including importing the csv module, creating, writing, and reading CSV files. It explains the structure of CSV files, the use of delimiters, and methods for handling newline characters to avoid empty rows in outputs. Additionally, it includes practical examples and exercises for creating and manipulating CSV files.

Uploaded by

Muskaan Arora
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views24 pages

CH 5 - CSV Files

This document provides an overview of working with CSV (Comma-Separated Values) files in Python, including importing the csv module, creating, writing, and reading CSV files. It explains the structure of CSV files, the use of delimiters, and methods for handling newline characters to avoid empty rows in outputs. Additionally, it includes practical examples and exercises for creating and manipulating CSV files.

Uploaded by

Muskaan Arora
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

CH 5 CSV File

CBSE – CLASS – XII


COMPUTER SCIENCE WITH PYTHON (NEW)
(SUBJECT CODE : 083)
Objectives 2

 Import csv module.


 Open and close csv file.
 Write into a csv file writer(),writerow(),writerows().
 Read from a csv fie using reader().
CSV File (Comma-Separated Values) 3
 CSV is a simple file format used to store tabular data, such as a
spreadsheet or database.
 CSV file are flat ,text files

 Files in the CSV format can be imported to and exported from programs
that store data in tables, such as Microsoft Excel or OpenOffice Calc.

 A csv file is a delimited text file that uses a comma to separate values.
 Each line of the file is a data record.
 Each record consists of one or more fields, separated by commas. The use
of the comma as a field separator is the source of the name for this file
format
What is delimiter in CSV File 4
 The separator character of CSV files is called delimiter.
 Default delimiter is comma
 Other popular delimiter include the
 tab(\t),
 colon(:),
 pipe(|),
 semicolon(;)
The csv files are popular because of 5
these reasons(Advantages):
I. Easier to create.
II. Preferred export and import format for databases and spreadsheets
III. Capable of storing large amount of data
How to create CSV file 6

 Method 1 (From MS-Excel):


 Open Excel, delete all the sheet except sheet 1
 Type all the data, in separate cells
 Save it as csv(comma delimited) file in your desired location.
 If any warning comes, click on “YES‟
 When you close the excel, choose “NO‟
 Now file is created at your desired location, go and double click or
open with notepad to check the content
How to create CSV file 7

 Method 2 (From Notepad):


 Open Notepad
 Type record by separating each column value by comma(,)
 Every record in separate line
 Save it by giving extension .csv (PUT THE NAME IN DOUBLE QUOTES TO ENSURE
.TXT WILL NOT BE APPENDED WITH FILE NAME) FOR E.G. if you want it to save
with name emp then give name as “emp.csv” in double quotes
 File is created close it and double click to open and check
w, w+ ,a ,a+ mode in csv file 8

 New csv file will be created when opened in an output file mode and if it
does not exist already.

 That is for the file file modes ,”w”,”w+”,”a”,”a+”,the file will be created
if does not exist already

 if it exists already, then the file modes “w”,”w+”,will overwrite the existing
file and the file mode “a” or “a+” will retain/append the contents of the file.
For writing into csv files, there are 9

three key players functions:

1.csv.writer()
2.<writerobject>.writerow()
3.<writerobject>.writerows()
For writing into csv files, there are 10

three key players functions:

1.csv.writer() Returns a writer object which


writes data into csv file
2.<writerobject>.writerow() Write one row of data onto the
writer object
3.<writerobject>.writerows() Write multiple rows of data onto
the writer object
.<writerobject>.writerow()
Q.Write a program to create a CSV file to store 11
student data (Rollno,Name,Marks).Obtain data from
user and write 5 record into the file.
<writerobject>.writerows()
Q.The data of winners of four rounds of a competitive programming 12
competition is given as below:
[‘Name’,’Points’,’Rank’]
[‘shradha’,4500,23]
[‘nischai’,7800,25]
[‘Ali’,6700,26]
[‘Adi’,3600,14]
Write a program to create a csv file (student.csv) and write the above
data into it.
13
 Reading from CSV file

import csv module


Use open() to open csv file, it will return
file object.
Pass this file object to reader object.
Perform operation you want
Q.Write a program to read the records of student.csv file
created before and display them 14

Notice the empty rows coming in the output. We didn’t specify the
newline argument in the file open method while creating/writing this
csv file and thus EOL translation took place, which resulted in the blank
rows after every record while reading.
The above situation can be handled by 2 ways.
15
Method-1:Suppressing the EOL character
Create/write onto csv file with newline=‘’ argument in
open method so that this situation do not arise.
The role of newline argument is to specify how would Python handle newline character while
working with CSV files.

Method-2
To read from a file which is created with EOL
translation (i.e no newline argument),open it with
newline argument set to the EOL character of the os
where the csv file was created.
For windows ,EOL is ‘\r\n’
Method-1
Create/write onto csv file with newline=‘’ argument in open method so that this 16
situation do not arise.
Method-2
17
To read from a file which is created with EOL translation (i.e no newline argument),open it
with newline argument set to the EOL character of the os where the csv file was created.
For windows ,EOL is ‘\r\n’
Do it yourself:
18
Write a program to create a csv(emp.csv) file by suppressing
the EOL translation.
Also write code to read it.

Data as follows
[‘empno’,’name.,’designation’,’salary’]
19
Delimiter argument of writer() and 20
reader() function
Import csv
L=[10,20,30,40,50]
Fh=open(“file.csv”,”w”,newline=‘’)
Wh=csv.writer(Fh,delimiter=“,”)
wh.writerow(l)
Fh.close()
Note:
Delimiter=‘,’ is default.
Q.Program to write data with ‘;’ as 21

a delimiter.
import csv
L=[10,20,30,40,50]
Fh=open(“file.csv”,”w”,newline=‘’)
wh=csv.writer(Fh,delimiter=“;”)
wh.writerow(L)
Fh.close()
Q. Write the output: 22
Output 23
Text Vs Binary Vs CSV Files 24
SL NO Operation Text File Binary File CSV File
1 Required Module - import pickle import csv
2 To read read() load() reader()
read(x)
readline()
readline(x)
readlines()
readlines(x)
3 To write write() dump() writer()
Writelines() writerow()
writerows()

You might also like