0% found this document useful (0 votes)
20 views28 pages

Csvfiles 2

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 28

CSV FILES

• A CSV file (Comma Separated Values file) isa


type of plain text file that uses specific
structuring to arrange tabulardata.
• 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,delimiter)
• Parameters:
➢ csvfile can be any object with a write() method. If csvfile is a file object, it should
be opened with newline=‘ ‘

➢ Delimiter to be given as delimiter=‘,’

csv.writer class provides two methodsfor writing to CSV.


They are writerow() and writerows().
WRITING ON TO THE CSV FILE
• writerow(): This method writes a single rowat a
time. Field row can be written using this method.
Syntax:
writerobject.writerow(fields)
• writerows(): This method is used to write multiple
rows at a time. This can beused to write rows list.
Syntax:
writerobject. writerows(rows)
Example : Writing data to CSV file

myfile.csv
BEFORE EXECUTION

myfile.csv
OUTPUT AFTER 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 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
• There are various classes provided by this
module for reading toCSV:
• Using csv.readerclass
• Using csv.Dictreaderclass
• 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, delimiter)
• Parameters:
csvfile: A file object for the CSV file.
Delimiter: The delimeter is to be given as
delimiter=‘,’
Example : Reading from CSV File

myfile.csv
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
CHANGING THE DELIMITER
def readstudent():
with open("Student.csv") as fin:
reader=csv.reader(fin, delimiter='/')
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']
WRITING DICTIONARY 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’)
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.
WRITING DICTIONARY 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(FIELD NAMES)
• writerows(): writerows method simply writes all the
rows but in each row, it writes only the values(not
keys).
Syntax:
• writerows(mydict)
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