Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 59

CHAPTER - VI

CSV FILES
INTRODUCTION

When you export data from Website,


the files can be delivered in the following
formats
1 CSV - Comma Separated Values

2 PSV - Pipe Separated Values

3 TSV - Tab Separated Values


1 CSV - COMMA SEPARATED VALUES

A CSV(Comma Separated Values) is a


plain-text file format used to store tabular
data such as a spreadsheet or a database.
It essentially stores a tabular data which
comprises of numbers and text into plain
text.
2 PSV - PIPE SEPARATED VALUES

When you export contact data and survey


data from the Gateway eNews letter and
Website systems, the files will be delivered in a
"pipe delimited" format. This means that the
various columns are separated by
a pipe character.
3 TSV - TAB SEPARATED VALUES

A text file format that uses tab characters


as separators between fields. The tab delimited
format stores information from a database or
spreadsheet in the format of a tabular
structure. Both Microsoft and Google allow the
user to convert a spreadsheet into tab
delimited format.
CSV FILES

What is a CSV File?


CSV FILES

What is a CSV File?

A CSV(Comma Separated Values) is a


plain-text file format used to store tabular
data such as a spreadsheet or a database.
It essentially stores a tabular data which
comprises of numbers and text into plain
text.
CSV FILES
CSV Files generally open into Excel and
nearly all the databases have different
csv
specific tools to allow the import of the
same.

Tablular data is represented by comma


seperated values
CSV FILES

STRUCTURE OF CSV FILE


WITH EXAMPLE
CSV FILES
For Example

Tabular data is represented by


comma separated values
STRUCTURE OF CSV FILE
RollNo,Name,Age,Class,Sec
1023,Vishwa,14,VII,A
3078,Varun,16,X,B
3543,Gowtham,15,X,C
Every line of the file is called a record. And
each record consists of fields that are separated
by commas which are also known as “delimiter”
which is the default delimiter, others include
pipe(|), semicolon(;). Given below is a structure
of a Normal CSV File separated by a comma
CSV FILE

Why is CSV File Format Used?


CSV FILE

Why is CSV File Format Used?

CSV is a plain-text file which makes it easier for


data interchange and also easier to import onto
spreadsheet or database storage.
For example: You might want to export the data of a certain
statistical analysis to CSV file and then import it to the
spreadsheet for further analysis. Overall it makes users working
experience very easy programmatically. Any language
supporting a text file or string manipulation like Python can
work with CSV files directly.
CSV FILES

Advantages
ADVANTAGES OF CSV FILES
CSV FILES - ADVANTAGES

ADVANTAGE ADVANTAGE ADVANTAGE ADVANTAGE


01 02 03 04
CSV is
CSV is
human CSV is CSV is
processed by
readable and faster to smaller in
almost all
easy to edit handle size
existing
manually
applications
CSV FILES - ADVANTAGES

ADVANTAGE ADVANTAGE ADVANTAGE ADVANTAGE


05 06 07 08
CSV is
CSV provides CSV is
considered straightforward
CSV is
simple to
to be information easy to
implement
standard schema generate
and parse
format
CSV FILES

DISADVANTAGES
DISADVANTAGES OF CSV FILES
CSV FILES - DISADVANTAGES

01 02 03

CSV allows to move There is no


No standard
most basic data only. distinction
way to
Complex between text
represent
configurations and numeric
binary data
cannot be imported values
and exported this
way
CSV FILES - DISADVANTAGES

04 05 06

Poor support of No standard


Lack of
special characters way to
universal
represent
standard
control
characters
CS
V CSV FILE TYPES
File
s
CS
V CSV FILE TYPES
File
s
1 CSV (Comma delimited)

2 CSV UTF-8 (Comma delimited)

3 CSV (Macintosh)

4 CSV (MS-DOS)
1 CSV (COMMA DELIMITED)

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.
Exporting to CSV uses a default
encoding of Unicode (UTF-16le)
2 CSV UTF-8 (COMMA DELIMITED)

UTF-8 (Unicode Transformation Format


-8 ) encoding, also referred to as "Unicode
- UTF8" UTF-8 encoded CSV files will work
well with Accompa (Accompa is a leading
cloud-based requirements management
software that helps you capture, track and
manage requirements for your products &
projects).
2 CSV UTF-8 (COMMA DELIMITED)

just English characters, or also contain non-


English characters such as é, ç, ü
3 CSV (MACINTOSH)

CSV (Macintosh) .csv. Saves a workbook


as a comma-delimited text file for use on
the Macintosh operating system, and ensures
that tab characters, line breaks, and other
characters are interpreted correctly.
4 CSV (MS-DOS)

CSV (DOS).csv. Saves a workbook as a


comma-delimited text file for use on
the Macintosh operating system, and ensures
that tab characters, line breaks, and other
characters are interpreted correctly.
CS
V PYTHON CSV MODULE
File
s
PYTHON CSV MODULE

Python uses a CSV package which is a


part of the standard library, so you need not
install it.
PYTHON CSV MODULE
Python CSV package contains following
functions:
Ser Functions Description
csv.field_size_lim It returns the maximum
1 it field size
Fetches the dialect
2 csv.get_dialect associated with name
Displays all the registered
3 csv.list_dialects dialects
4 csv.reader Read data from csv file
PYTHON CSV MODULE
Python CSV package contains following
functions:
Ser Functions Description
Dialect associated with a
5 csv.register_dialect name
6 csv.writer Writes data to a csv file
It deletes the dialect
csv.unregister_dial associated with the
7 ect name dialect registry
Quotes everything
8 csv.QUOTE_ALL irrespective of the type
PYTHON CSV MODULE
Python CSV package contains following
functions:
Ser Functions Description
Quotes special
9 csv.QUOTE_MINIMAL character field
Quotes fields that
10 csv.QUOTE_NONNUMERIC are not numeral
Doesn’t quote
11 csv.QUOTE_NONE anything in output
CS
V
File
s

CSV FILE OPERATIONS IN PYTHON


CSV FILE OPERATIONS IN PYTHON

You can perform several manipulations


once a CSV file is loaded. I am going to show
the read and write operations on a CSV file
in Python
READING AND WRITING FILES

The csv module is used for reading and


writing files. It mainly provides following
classes and functions:
READING AND WRITING FILES

1 reader() METHOD

2 writer() METHOD

3 DictReader() METHOD

4 DictWriter() METHOD
READING AND WRITING FILES

1 reader() METHOD
1 reader() METHOD

The reader() function takes a file object


and returns a _csv.reader object that
can be used to iterate over the contents
of a CSV file. The syntax of reader()
function is as follows:

Syntax: reader(fileobj [, dialect='excel' [,


**fmtparam] ]) -> _csv.reader
1 reader() METHOD
Argument Description
fileobj (required) It refers to the file object
dialect (optional) Dialect refers to the
different ways of formatting the CSV
document. By default, the csv module
uses the same format as Microsoft
Excel.
fmtparam (optional) It refers to the set of
keyword arguments to customize the
dialect.
1
1 reader() METHOD

Let's say we have following CSV file:

stud2020.csv
1 reader() METHOD
1 reader() METHOD

OUT
PUT
1 reader() METHOD

PRINTING SELECTED FIELDS


1 reader() METHOD

PRINTING SELECTED FIELDS


1 reader() METHOD

PRINTING SELECTED FIELDS

OUT
PUT
2 writer() METHOD
OUT
PUT
2 writer() METHOD
OUT
PUT
To write data to a CSV file we use
the writer() function. It accepts the same argument
as the reader() function but returns a writer object
(i.e _csv.writer):

Syntax: writer(fileobj [, dialect='excel' [,


**fmtparam] ]) -> csv_writer
2 writer() METHOD
OUT
PUT
ARGUMENT DESCRIPTION
fileobj (required) It refers to the file object
dialect (optional) Dialect refers to the different
ways of formatting the CSV document. By
default, the csv module uses the same
format as Microsoft Excel. We will discuss
dialect in detail later in this post.

fmtparam (optional) Formatting parameters, work


same as the reader()'s function.
2

OUT
PUT
THE WRITER INSTANCE METHODS
THE WRITER INSTANCE METHODS
The writer instance provides the following two
methods to write data:

Method Description
writerow(row) Writes a single row of data and
returns the number of characters
written. The row must be a
sequence of strings and number.
writerows(rows) Writes multiple rows of data and
returns None. The rows must be a
sequence.
2

THE WRITER INSTANCE METHODS

THE writerow(row) METHOD


THE WRITER INSTANCE METHODS
THE writerow(row) METHOD

Writes a single row of data and


returns the number of characters
written. The row must be a sequence of
strings and number.
THE writerow(row) METHOD
THE writerow(row) METHOD
OU
PU T
T
2

THE WRITER INSTANCE METHODS

THE writerows(rows) METHOD


THE WRITER INSTANCE METHODS
THE writerows(rows) METHOD

Writes multiple rows of data and


returns None. The rows must be a
sequence.
2

THE WRITER INSTANCE METHODS

THE writerows(rows) METHOD

THE writerows(rows) EXAMPLE


THE writerows(rows) EXAMPLE
THE writerows(rows) EXAMPLE
OU
PU T
T
3
DictReader() METHOD

4
DictWriter() METHOD

These two methods are beyond the


scope of syllabus.

You might also like