0% found this document useful (0 votes)
13 views11 pages

notes on CSV Filespdf

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

CSV files in Python

CSV (Comma Separated Values) is a simple file format used to store tabular data,
such as a spreadsheet or database. 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. The use of the comma as a field separator is the
source of the name for this file format.

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

Opening and Closing CSV Files


A CSV file is opened in the same way as we open any other text files but make sure
to do the following two things:
1. Specify the file extension as .csv
2. open the file like other text files e.g.
dfile=open (“student.csv”,”w”) ( csv file opened in write mode with the file handle as
dfile)
Or
File1=open(“student.csv”,’r’) ( csv file opened in read mode with the file handle as
file1)
As open CSV file is closed in the same manner as we close any other file i.e.,
dfile.close()
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
csv file object should be opened with newline='' otherwise newline characters
inside the quoted fields will not be interpreted correctly.

Syntax: csv.writer(csvfile, dialect=’excel’, **fmtparams)


Parameters:
csvfile: A file object with write() method.
dialect (optional): Name of the dialect to be used.
fmtparams (optional): Formatting parameters that will overwrite those specified in
the dialect.

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:
writerow(fields)
• 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
writerows(rows)

Example:
# Python program to demonstrate
# writing to CSV
import csv
# field names
fields = ['Name', 'Branch', 'Year', 'CGPA']

# data rows of csv file


rows = [ ['sahitya', 'COE', '2', '9.0'],['versha', 'COE', '2',
'9.1'],['Aditya', 'IT', '2', '9.3'],['Sagar', 'SE', '1',
'9.5'],['Akshat', 'MCE', '3', '7.8'],
['Pawan', 'EP', '2', '9.1']]

# name of csv file


filename = "university_records.csv"

# writing to csv file


with open(filename, 'w') as csvfile:
# creating a csv writer object
csvwriter = csv.writer(csvfile)

# writing the fields


csvwriter.writerow(fields)
# writing the data rows
csvwriter.writerows(rows)
Reading from CSV file
Python contains a module called csv for the handling of CSV files. The reader class from the
module is used for reading data from a CSV file. At first, the CSV file is opened using the
open() method in ‘r’ mode(specifies read mode while opening a file) which returns the file
object then it is read by using the reader() method of CSV module that returns the reader
object that iterates throughout the lines in the specified CSV document.
Syntax:
csv.reader(csvfile, dialect='excel', **fmtparams
Note: The ‘with‘ keyword is used along with the open() method as it simplifies exception
handling and automatically closes the CSV file.
Example:

Consider the below CSV file –

import csv

# opening the CSV file


with open('Giants.csv', mode ='r')as file:

# reading the CSV file


csvFile = csv.reader(file)

# displaying the contents of the CSV file


for lines in csvFile:
print(lines)
Output:
[['Steve', 13, 'A'],
['John', 14, 'F'],
['Nancy', 14, 'C'],
['Ravi', 13, 'B']]
Writing to CSV file
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
CSV file object should be opened with newline=” otherwise, newline characters
inside the quoted fields will not be interpreted correctly.
Syntax:
csv.writer(csvfile, dialect='excel', **fmtparams)
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:
writerow(fields)
• writerows(): This method is used to write multiple rows at a time. This can be
used to write rows list.
Syntax:
writerows(rows)
Example:
# Python program to demonstrate
# writing to CSV

import csv

# field names
fields = ['Name', 'Branch', 'Year', 'CGPA']

# data rows of csv file


rows = [ ['Sahitya', 'COE', '2', '9.0'],
['Pawan', 'COE', '2', '9.1'],
['Aditya', 'IT', '2', '9.3'],
['Akshat', 'SE', '1', '9.5'],
['Harshjeet', 'MCE', '3', '7.8'],
['Rishi', 'EP', '2', '9.1']]

# name of csv file


filename = "university_records.csv"

# writing to csv file


with open(filename, 'w') as csvfile:
# creating a csv writer object
csvwriter = csv.writer(csvfile)

# writing the fields


csvwriter.writerow(fields)

# writing the data rows


csvwriter.writerows(rows)

We can also write dictionary to the CSV file. For this the CSV module provides the
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)
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)
Example:
# 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'},
{'branch': 'MCE', 'cgpa': '7.8', 'name': 'Prateek', 'year':
'3'},
{'branch': 'EP', 'cgpa': '9.1', 'name': 'Sahil', 'year':
'2'}]

# field names
fields = ['name', 'branch', 'year', 'cgpa']

# name of csv file


filename = "university_records.csv"

# writing to csv file


with open(filename, 'w') as csvfile:
# creating a csv dict writer object
writer = csv.DictWriter(csvfile, fieldnames = fields)

# writing headers (field names)


writer.writeheader()

# writing data rows


writer.writerows(mydict)

The CSV format is the most commonly used import and export format for databases
and spreadsheets.
Reader and Writer Modules
The CSV module has several functions and classes available for reading and writing CSVs, and they
include:

• csv.reader function
• csv.writer function
• csv.Dictwriter class
• csv.DictReader class

csv.reader
The csv.reader module takes the following parameters:

• csvfile : This is usually an object which supports the iterator protocol and
usually returns a string each time its __next__() method is called.
• dialect='excel' : An optional parameter used to define a set of
parameters specific to a particular CSV dialect.
• fmtparams : An optional parameter that can be used to override existing
formatting parameters.

Here is an example of how to use the csv.reader module.

import csv

with open('example.csv', newline='') as File:

reader = csv.reader(File)

for row in reader:

print(row)

csv.writer module
This module is similar to the csv.reader module and is used to write data to a CSV. It takes three
parameters:

• csvfile : This can be any object with a write() method.


• dialect='excel' : An optional parameter used to define a set of
parameters specific to a particular CSV.
• fmtparam : An optional parameter that can be used to override existing
formatting parameters.

Reading a CSV File


Let's see how to read a CSV file using the helper modules we have discussed above.

Create your CSV file and save it as example.csv. Ensure that it has the .csv extension and fill in
some data. Here we have our CSV file which contains the names of students and their grades.
Below is the code for reading the data in our CSV using both the csv.reader function and
the csv.DictReader class.

Reading a CSV File With csv.reader


import csv
with open('example.csv') as File:
reader = csv.reader(File, delimiter=',', quotechar=',',
quoting=csv.QUOTE_MINIMAL)
for row in reader:
print(row)
In the code above, we import the CSV module and then open our CSV file as File . We then define
the reader object and use the csv.reader method to extract the data into the object. We then iterate
over the reader object and retrieve each row of our data.
We show the read data by printing its contents to the console. We have also specified the required
parameters such as delimiter, quotechar, and quoting.

Output

['first_name', 'last_name', 'Grade']


['Alex', 'Brian', 'B']
['Rachael', 'Rodriguez', 'A']
['Tom', 'smith', 'C']

Reading a CSV File With DictReader


As we mentioned above, DictWriter allows us to read a CSV file by mapping the data to a dictionary
instead of strings as in the case of the csv.reader module. Although the fieldname is an optional
parameter, it's important to always have your columns labelled for readability.

Here's how to read a CSV using the DictWriter class.

import csv
results = []
with open('example.csv') as File:
reader = csv.DictReader(File)
for row in reader:
results.append(row)
print results
We first import the csv module and initialize an empty list results which we will use to store the data
retrieved. We then define the reader object and use the csv.DictReader method to extract the data
into the object. We then iterate over the reader object and retrieve each row of our data.
Finally, we append each row to the results list and print the contents to the console.

Output

[{'Grade': 'B', 'first_name': 'Alex', 'last_name': 'Brian'},


{'Grade': 'A', 'first_name': 'Rachael', 'last_name': 'Rodriguez'},
{'Grade': 'C', 'first_name': 'Tom', 'last_name': 'smith'},
{'Grade': 'B', 'first_name': 'Jane', 'last_name': 'Oscar'},
{'Grade': 'A', 'first_name': 'Kennzy', 'last_name': 'Tim'}]
As you can see above, using the DictReader class is better because it gives out our data in a
dictionary format which is easier to work with.
Writing to a CSV File
Let's now see how to go about writing data into a CSV file using the csv.writer function and
the csv.Dictwriter class discussed at the beginning of this tutorial.

Writing to a CSV File Using csv.writer


The code below writes the data defined to the example2.csv file.

import csv
myData = [["first_name", "second_name", "Grade"],
['Alex', 'Brian', 'A'],
['Tom', 'Smith', 'B']]
myFile = open('example2.csv', 'w')
with myFile:
writer = csv.writer(myFile)
writer.writerows(myData)
print("Writing complete")

First we import the csv module, and the writer() function will create an object suitable for writing. To
iterate the data over the rows, we will need to use the writerows() function.

Here is our CSV with the data we have written to it.

Writing to a CSV File Using DictWriter


Let's write the following data to a CSV

data = [{'Grade': 'B', 'first_name': 'Alex', 'last_name': 'Brian'},

{'Grade': 'A', 'first_name': 'Rachael', 'last_name': 'Rodriguez'},

{'Grade': 'C', 'first_name': 'Tom', 'last_name': 'smith'},

{'Grade': 'B', 'first_name': 'Jane', 'last_name': 'Oscar'},

{'Grade': 'A', 'first_name': 'Kennzy', 'last_name': 'Tim'}]

The code is as shown below.

import csv
with open('example4.csv', 'w') as csvfile:
fieldnames = ['first_name', 'last_name', 'Grade']
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
writer.writerow({'Grade': 'B', 'first_name': 'Alex',
'last_name': 'Brian'})
writer.writerow({'Grade': 'A', 'first_name': 'Rachael',
'last_name': 'Rodriguez'})
writer.writerow({'Grade': 'B', 'first_name': 'Jane',
'last_name': 'Oscar'})
writer.writerow({'Grade': 'B', 'first_name': 'Jane',
'last_name': 'Loive'})
print("Writing complete")
We first define the fieldnames , which will represent the headings of each column
in the CSV file. The writerrow() method will write to one row at a time. If you
want to write all the data at once, you will use the writerrows() method.
Here is how to write to all the rows at once.

import csv

with open('example5.csv', 'w') as csvfile:


fieldnames = ['first_name', 'last_name', 'Grade']
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
writer.writerows([{'Grade': 'B', 'first_name': 'Alex',
'last_name': 'Brian'},{'Grade': 'A', 'first_name':
'Rachael','last_name': 'Rodriguez'},{'Grade': 'C', 'first_name':
'Tom', 'last_name': 'smith'},{'Grade': 'B', 'first_name': 'Jane',
'last_name': 'Oscar'},{'Grade': 'A', 'first_name': 'Kennzy',
'last_name': 'Tim'}])
print("writing complete")
The CSV file or comma separated values file are one of the most widely used flat
files to store and hare data across platforms. The columns are separated by comma
and there is optional header row also which will indicate the name of each
column. Python can read the CSV files using many modules. In this article we will
see how the CSV library in python can be used to read and write a CSV file. We can
also see the pandas library onl to read the CSV file.
Reading CSV file using csv module
We can get the CSV file from (https://www.guru99.com/python-csv.html)
Example

import csv
with open('C:\\iris.csv','rt')as file:
csv_rows = csv.reader(file)
for row in csv_rows:
print(row)
Output
Running the above code gives us the following result −

['sepal.length', 'sepal.width', 'petal.length', 'petal.width',


'variety']
['5.1', '3.5', '1.4', '.2', 'Setosa']
['4.9', '3', '1.4', '.2', 'Setosa']
['4.7', '3.2', '1.3', '.2', 'Setosa']
['4.6', '3.1', '1.5', '.2', 'Setosa']
['5', '3.6', '1.4', '.2', 'Setosa']
['5.4', '3.9', '1.7', '.4', 'Setosa']
['4.6', '3.4', '1.4', '.3', 'Setosa']

Writing CSV file using csv module


To create a csv file we use Python lists we declare a data set containing each row as
a list and all the rows are sub list in a big singer list. We also create another data set
which just represents the header row. Then we use various methods like writerow()
and csv.writer to finally write the file into the local system.
Example
import csv
data = ["Month", "1958", "1959", "1960"]
x = [
["JAN", 340, 360, 417],["FEB", 318, 342, 391],["MAR", 362,
406, 419],["APR", 348, 396, 461],["MAY", 363, 420, 472],
["JUN", 435, 472, 535],["JUL", 491, 548, 622],["AUG", 505,
559, 606],["SEP", 404, 463, 508],["OCT", 359, 407,
461],["NOV", 310, 362, 390],["DEC", 337, 405, 432]]
y = "C:\\years.csv"
with open(y, 'w') as work:
z = csv.writer(work)
z.writerow(data)
z.writerows(x)
Output
Running the above code gives us the following result −

Month,1958,1959,1960
JAN,340,360,417
FEB,318,342,391
MAR,362,406,419
APR,348,396,461
MAY,363,420,472
JUN,435,472,535
JUL,491,548,622
AUG,505,559,606
SEP,404,463,508
OCT,359,407,461
NOV,310,362,390
DEC,337,405,432
Writing CSV file using pandas
Using pandas is we create a data frame which country is the rows as well as the
headers of the rows. Then we use the to_csv method which text the filename and
path as parameters and drives the data to csv file.
Example

from pandas import DataFrame


C = {'Month': ['JAN','FEB', 'MAR'],
'1958': ['345', '435', '545'],
'1959': ['377', '135', '985'],
'1960': ['498', '354', '765'],
}
df = DataFrame(C, columns= ["Month", "1958", "1959", "1960"])
export_csv = df.to_csv (r'C:\\years_p.csv', index = None,
header=True) # here you have to write path, where result file
will be stored
print (df)
Output
Running the above code gives us the following result −

Month 1958 1959 1960


0 JAN 345 377 498
1 FEB 435 135 354
2 MAR 545 985 765

You might also like