notes on CSV Filespdf
notes on CSV Filespdf
notes on CSV Filespdf
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
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.
Example:
# Python program to demonstrate
# writing to CSV
import csv
# field names
fields = ['Name', 'Branch', 'Year', 'CGPA']
import csv
import csv
# field names
fields = ['Name', 'Branch', 'Year', 'CGPA']
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']
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.
import csv
reader = csv.reader(File)
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:
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.
Output
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
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.
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
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 −
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