0% found this document useful (0 votes)
583 views16 pages

CSV File PPT - Intro

The document discusses CSV (comma-separated values) file format. It states that CSV is a simple flat file format used to store tabular data like spreadsheets. Each line in a CSV file represents a data record with fields separated by commas. The CSV format allows data to be imported and exported from programs that work with tables like Excel.

Uploaded by

dca140107
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)
583 views16 pages

CSV File PPT - Intro

The document discusses CSV (comma-separated values) file format. It states that CSV is a simple flat file format used to store tabular data like spreadsheets. Each line in a CSV file represents a data record with fields separated by commas. The CSV format allows data to be imported and exported from programs that work with tables like Excel.

Uploaded by

dca140107
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

 CSV is a simple flat 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“.
 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

2
3
4

▸ 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
5
6
Python provides a module to deal with these variations
called the csv module

This module allows you to read spreadsheet info into


our program

We load the module in the usual way using import:

7
 A CSV file stores tabular data (numbers and text) in plain text.
* Each line of the file is a data record.
* Each record consists of one or more fields, separated by commas.
* CSV files allows to choose a different delimiter character such as

‘\t’, ‘:’ , ‘;’ , ‘|’. but the default delimiter is ‘,’comma.


* To read and write in CSV files we need to import csv module.
* csv module provides two specific objects -reader and writer to read
and write into the file.
* These objects read and write delimited sequence as records

in a csv file. 8
Opening csv file:

A csv file is opened n the same way as any text file is


opened.

Syntax:
fh=open(“[Link]”,”w”)- to open the file in write mode
fh=open(“[Link]”,”r”)- to open the file in write mode

9
* Import csv module
* Open csv file in a file handle
* Create a reader object by using [Link]() function
Example: stureader=[Link](fh)
* using for loop fetch the data from the reader object in
which data is stored in the form of iterable

10
import csv
f=open("d:/[Link]",'r')
csvr = [Link](f)
for row in csvr:
print(row)
[Link]()

11
 Import csv module
* Open csv file in a file handle( just as text file)
Example : fh=open(“[Link]”,”w”)- to open the file in
write mode
* Create a writer object by using [Link]() function.
Ex. stuwriter=[Link](fh)
* Obtain user data and form a Python sequence
 (list or tuple) out of it.
Ex. sturec=(11, ‘Neelam’,79.0)
* Write the Python sequence onto the writer object
using [Link]() or [Link]()
Ex. [Link](sturec)
12
Write to a CSV file

import csv
with open('[Link]', 'w', newline='') as file:
writer = [Link](file)
[Link](["SN", "Movie", "Protagonist"])
[Link]([1, "Lord of the Rings", "Frodo Baggins"])
[Link]([2, "Harry Potter", "Harry Potter"])

13
Writing multiple rows with writerows()

import csv
csv_rowlist = [["SN", "Movie", "Protagonist"],
[1, "Lord of the Rings", "Frodo Baggins"],
[2, "Harry Potter", "Harry Potter"]]
with open('[Link]', 'w') as file:
writer = [Link](file)
[Link](csv_rowlist)
14
CSV FILE

15
16

CSV FILES
2
CSV is a simple flat file format used to store tabular data, such as
a spreadsheet or database.
Files in the CSV format c
3FEATURES OF A CSV FILE
4PROBLEM OF HANDLING HUGE DATACSV AS A SOLUTION
“
▸CSV is faster to handle.
• CSV is smaller in size.
• CSV is easy to generate and import onto a                   
spreadsh
6CSV FILE
7CSV FORMAT
Python provides a module to deal with these variations 
called  the csv module
This module allows you to read s
8CSV FILE
A CSV file stores tabular data (numbers and text) in plain text.
* Each line of the file is a data record.
* Each
9
Opening csv file:
A csv file is opened n the same way as any text file is 
opened.
Syntax:
fh=open(“stu.csv”,”w”)-
to open
10Reading from CSV file
* Import csv module
* Open csv file in a file handle
* Create a reader object by using csv.reader() f

You might also like