0% found this document useful (0 votes)
49 views37 pages

CSVFILES

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

CSV FILES

• A CSV file (Comma Separated Values file) is a


type of plain text file that uses specific
structuring to arrange tabular data.
• It can contain only actual text data—in other
words, printable ASCII or Unicode characters.
• A CSV file stores tabular data (numbers and
text) in plain text.
CSV
FILE
CSV is a simple file format used to store tabular data, such as
a spreadsheet or database.
 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.
 CSV stands for "comma-separated values“.
 A comma-separated values 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
Each line in a file is known as
data/record. Each record consists of
one or more fields, separated by
commas (also known as delimiters), i.e.,
each of the records is also a part of this
file. Tabular data is stored as text in a
CSV file. The use of comma as a field
separator is the source of the name for
this file format. It stores our data into a
spreadsheet or a database.

CSV files are commonly used because they are easy to read and manage, small in size,
and fast to process/transfer. Because of these salient features, they are frequently used
in software applications, ranging anywhere from online e-commerce stores to mobile
apps to desktop tools. For example, Magento, an e-commerce platform, is known for its
support of CSV.
Thus, in a nutshell, the several advantages that are offered by CSV files are as follows:
• CSV is faster to handle.
• CSV is smaller in size.
• CSV is easy to generate and import onto a spreadsheet or database.
• CSV is human readable and easy to edit manually.
• CSV is simple to implement and parse.
• CSV is processed by almost all existing applications.
Format of a CSV file :

column name 1, column name 2, column name 3


first row data 1, first row data 2, first row
data 3
second row data 1, second row data 2, second row
data 3
• In general, the separator character is called a delimiter, and the
comma is not the only one used. Other popular delimiters include
the tab (\t), colon (:) and semi-colon (;) characters.
• CSV files are normally created by programs that handle large
amounts of data. They are a convenient way to export data from
spreadsheets and databases as well as import or use it in other
programs.
CSV file handling in
Python
 To perform read and write operation with CSV
file, we must import csv module.
 open() function is used to open file, and return file
object.
Writing date in CSV file
Python provides an in-built module called csv to
work with CSV files.

There are various classes provided by this


module for writing to CSV:

•Using csv.writer class

•Using csv.DictWriter class


Writing date in CSV file
 import csv module
 Use open() to open CSV file by specifying mode
“w” or “a”, it will return file object.
 “w” will overwrite previous content
 “a” will add content to the end of previous
content.
 Pass the file object to writer object with
delimiter.
 Then use writerow() to send data in CSV file
WRITING ON TO THE CSV FILE
Using csv.writer class

 csv.writer class is used to insert data to the CSV file.

 This class returns a writer object which is responsible


for converting the user’s data into a delimited string.

 A csvfile object should be opened with newline=“ “


otherwise newline characters inside the quoted fields will
not be interpreted correctly.
WRITING ON TO THE CSV FILE
• Syntax:
writerobject=csv.writer(csvfile, dialect='excel', **fmtparams)
• Parameters:
 csvfile can be any object with a write() method. If csvfile is a file object, it should
be opened with newline=“ “

 An optional dialect parameter can be given which is used to define a set of


parameters specific to a particular CSV dialect. It may be an instance of a subclass
of the Dialect class or one of the strings returned by the list_dialects() function.

 The other optional fmtparams keyword arguments can be given to override


individual formatting parameters in the current dialect.
WRITING ON TO THE CSV FILE
• csv.writer class provides two methods for
writing to CSV. They are writerow() and
writerows().
• writerow(): This method writes a single row
at a time. Field row can be written using this
method.
Syntax:
writerobject.writerow(fields)
WRITING ON TO THE CSV FILE
• writerows(): This method is used to write
multiple rows at a time. This can be used
to write rows list.
Syntax:
Writing CSV files in Python
writerobject. writerows(rows)
Example : Writing data to CSV
file

BEFORE
EXCUTION

OUTPU AFTER
T EXECUTION
Example : Writing data to CSV
file

myfile.cs
v
Example : Writing data to CSV
file
myfile.csv
BEFORE
EXECUTION

myfile.csv
OUTPU AFTER
T EXECUTION
EXAMPLE PROGRAM TO WRITE ON TO THE CSV FILE

import csv
fields=['Rno','Name','Avg']
rows=[ ['1', 'Abhishek' , '98'],
Content of Student.csv
['2', 'Anand', '99'], Rno,Name,Avg
['3', 'Ravi', '88']]
1,Abhishek,98
with open("Student.csv","w") as fout:
csvwriter=csv.writer(fout) 2,Anand,99
csvwriter.writerow(fields)
3,Ravi,88
print("Column headings written")
csvwriter.writerows(rows)
EXAMPLE PROGRAM FOR WRITING ON TO THE FILE
EXAMPLE PROGRAM FOR WRITING ON TO THE FILE

Contents of “marks.csv” created:


Reading from CSV
•fileimport csv module
• Use open() to open csv file, it will return file object.
• Pass this file object to reader object.
• Perform operation you want
• Python provides an in-built module called
csv to work with CSV files.
• There are various classes provided by this
module for writing to CSV:
• Using csv.reader class
• Using csv.Dictreader class
USING csv.reader CLASS

• csv.reader class is used to extract data from


the CSV file.

• This class’s method csv.reader() returns a


reader object which can be iterated over
to retrieve the lines of CSV file.

• The data is read as a list of strings.


SYNTAX OF THE FUNCTION
CSV.READER()
• csv.reader(csvfile, dialect=’excel’, fmtparams)
• Parameters:
csvfile: A file object for the CSV file.
dialect (optional): Name of the dialect to be
used.
fmtparams (optional): Formatting parameters
that will overwrite those specified in the dialect.
Example : Reading from CSV
File

myfile.cs
v
Example : Counting number of
records

myfile.csv
Example : Sum of Salary and counting
employee getting more than 70000
Example : Sum of Salary and counting
employee getting more than 70000

myfile.csv
DIALECT PARAMETER IN
READER() AND WRITER()
• It is a construct that allows to create, store and
re-use various formatting parameter for the data.
• Python offers 2 ways to specify formatting
parameters.
(i) By declaring a sub class of this class which
contains the specific attributes.
(ii) By directly specifying the formatting
parameters, using the same names as defined in
the Dialect class.
DIALECT PARAMETER IN READER() AND WRITER()
Dialects support the following attributes:

Dialect.delimiter A one-character string used to separate fields. It defaults to ','.

Dialect.doublequote Controls how instances of quotechar appearing inside a field


should themselves be quoted. When True, the character is doubled. When False,
the escapechar is used as a prefix to the quotechar. It defaults to True.
On output, if doublequote is False and no escapechar is set, Error is raised if a
quotechar is found in a field.

Dialect.escapechar A one-character string used by the writer to escape the


delimiter if quoting is set to QUOTE_NONE and the quotechar if doublequote is False
. On reading, the escapechar removes any special meaning from the following
character. It defaults to None, which disables escaping.

Dialect.lineterminator The string used to terminate lines produced by the writer.


It defaults to '\r\n'.
DIALECT PARAMETER IN READER() AND WRITER()
The reader is hard-coded to recognise either '\r' or '\n' as end-of-line, and
ignores lineterminator. This behavior may change in the future.

Dialect.quotechar A one-character string used to quote fields containing


special characters, such as the delimiter or quotechar, or which contain new-line
characters. It defaults to '"'.

Dialect.quoting Controls when quotes should be generated by the writer and


recognised by the reader. It can take on any of the QUOTE_* constants (see
section Module Contents) and defaults to QUOTE_MINIMAL.

Dialect.skipinitialspace When True, whitespace immediately following the


delimiter is ignored. The default is False.

Dialect.strict When True, raise exception Error on bad CSV input. The default is
False.
EXAMPLE PROGRAM TO READ FROM
THE CSV FILE
def readstudent():
with open("Student.csv") as fin:
reader=csv.reader(fin)
for row in reader:
print(row)
readstudent()
OUTPUT:
['Rno', 'Name', 'Avg']
['1', 'Abhishek', '98']
['2', 'Anand', '99']
['3', 'Ravi', '88']
CHANGING THE DELIMITER
def readstudent():
with open("Student.csv") as fin:
reader=csv.reader(fin, delimiter='/',
quoting=csv.QUOTE_NONE)
for row in reader:
print(row)
readstudent()

Content of Student.csv OUTPUT:


'Rno','Name','Avg' ["'Rno','Name','Avg'"]
1/Abhishek/98 ['1', 'Abhishek', '98']
2/Anand/98 ['2', 'Anand', '98']
3/AAAA/88 ['3', 'AAAA', '88']
CREATING A DIALECT
• The csv module allows us to create a dialect with the specific
characteristics of our CSV file using the function register_dialect().
def readstudent():
csv.register_dialect ("myDialect",
delimiter='/',quoting=csv.QUOTE_NONE)

OUTPUT:
with open("Student1.csv") as fin: reader=csv.reader
["'Rno','Name','Avg'"]
(fin, dialect="myDialect") for row in reader: ['1', 'Abhishek', '98']
print(row) ['2', 'Anand', '98']
['3', 'AAAA', '88']
readstudent()

* The output has only comma(,) seperated values which


means we correctly parsed the non-standard CSV data
WRITING ON TO THE CSV FILE
• csv.DictWriter provides two methods for writing to
CSV. They are:
• writeheader(): writeheader() method simply writes
the first row of your csv file using the pre-specified
fieldnames.
Syntax:
• writeheader()
• writerows(): writerows method simply writes all the
rows but in each row, it writes only the values(not
keys).
Syntax:
• writerows(mydict)
WRITING ON TO THE CSV FILE
• Using csv.DictWriter class
• This class returns a writer object which maps dictionaries onto
output rows.
• Syntax: csv.DictWriter(csvfile, fieldnames, restval=”,
extrasaction=’raise’, dialect=’excel’, *args, **kwds)
Parameters:
csvfile: A file object with write() method.
fieldnames: A sequence of keys that identify the order in which
values in the dictionary should be passed.
restval (optional): Specifies the value to be written if the
dictionary
is missing a key in fieldnames.
extrasaction (optional): If a key not found in fieldnames, the
optional extrasaction parameter indicates what action to take. If it is
set to raise a ValueError will be raised.
dialect (optional): Name of the dialect to be used.
EXAMPLE PROGRAM FOR WRITING ON TO THE FILE
# importing the csv module
import csv
# my data rows as dictionary objects
mydict =[{'branch': 'COE', 'cgpa': '9.0', 'name': 'Nikhil', 'year': '2'},
{'branch': 'COE', 'cgpa': '9.1', 'name': 'Sanchit', 'year': '2'},
{'branch': 'IT', 'cgpa': '9.3', 'name': 'Aditya', 'year': '2'},
{'branch': 'SE', 'cgpa': '9.5', 'name': 'Sagar', 'year': '1'}, Content of
{'branch': 'MCE', 'cgpa': '7.8', 'name': 'Prateek', 'year': '3'}, university_records1.csv
{'branch': 'EP', 'cgpa': '9.1', 'name': 'Sahil', 'year': '2'}] name,branch,year,cgpa
# field names Nikhil,COE,2,9.0
fields = ['name', 'branch', 'year', 'cgpa']
# name of csv file
Sanchit,COE,2,9.1
filename = "university_records1.csv" Aditya,IT,2,9.3
# writing to csv file Sagar,SE,1,9.5
with open(filename, 'w') as csvfile: Prateek,MCE,3,7.8
# creating a csv dict writer object Sahil,EP,2,9.1
writer = csv.DictWriter(csvfile,
fieldnames = fields)
# writing headers (field names)
writer.writeheader()
# writing data rows
writer.writerows(mydict)
USING DictReader class
• Assume that the file named
countries.csv has the content –
country, capital
France, Paris Italy,
Rome Spain,
Madrid Russia,
Moscow India,
New Delhi
READING FROM A CSV FILE
USING DICTREADER CLASS
with open('countries.csv') as fin:
reader=csv.DictReader(fin)
for row in reader:
print(row['country'])

OUTPUT:
France
Italy Spain
Russia
India
EXAMPLE PROGRAM USING DICTREADER
• Write a python program to read from the file
emp.csv and display the name of the employees
whose salary >50000

with open("emp.csv“,newline=‘ ‘) as fin: Content of emp.csv :


Eno,Ename,Salary
reader=csv.DictReader(fin) 1, ABC, 40000
for row in reader: 2, XYZ, 60000
3, LMN, 95000
if(int(row['Salary'])>60000):
print(row['Ename']) Output :
LMN

You might also like