How to read CSV file in Python
In this article, we will learn different ways to read CSV files in Python. We will use some built-in modules and libraries available in Python and some related custom examples as well. We will compare text files and CSV files with examples. Let's first have a quick look over the full form of CSV, introduction to CSV, and then look at the following examples showing different parsing modules to read CSV documents in Python.
What is CSV?
CSV stands for Comma Separated Values
. The file uses a separator character called delimiter to separate each value. CSV is a typical format for information exchange as it's smaller, straightforward, and general. Each line of the file is a data record. The standard format of the data record is defined by rows and columns. Each record comprises one or more fields, separated by commas.
If we take a table having thousands of data, the .csv file has the ability to separate the values using commas into distinguishable fields or columns. Normally, first-line tells the heading or column name of data and after that actual data set is listed.
Sample CSV File
Model number,Price,Quantity,Company
ST001,35000,10,Samsung
RW345,46500,14,Onida
EX366,30000,8,Lenovo
FU699,45000,12,Acer
CSV Handling in python is required due to large data sets in python. The CSV library provides functionality to both reads from and write to CSV files and then analyzing the data using python libraries and their predefined functions.
Example: Reading CSV File using CSV Module
To read data from the CSV file, we must use the reader function to generate a reader object. We use python’s open()
function to open a text file, which returns a file object. This is then passed to the reader.
The csv.reader()
method returns a reader object which will iterate over each line in the given CSV file. Each row read from the CSV file is returned as a list of strings.
import csv
with open('model.csv') as file:
data = csv.reader(file)
for row in data:
print(row)
Model number,Price,Quantity,Company
ST001,35000,10,Samsung
RW345,46500,14,Onida
EX366,30000,8,Lenovo
FU699,45000,12,Acer
Example: Reading CSV File using CSV.DictReader() Function
Instead of printing a list of individual String elements, CSV data can be directly printed in the form of an ordered dictionary. The first line of the CSV file is assumed to contain the keys to use to build the dictionary.
The csv.DictReader()
function creates an object that operates like a regular reader but maps the information in each row to a dictionary whose keys are given by the optional fieldnames parameter.
#import necessary modules
import csv
data = csv.DictReader(open("model.csv"))
for row in data:
print(row)
OrderedDict([('Model number', 'ST001'), ('Price', '35000'), (' Quantity', ' 10'), (' Company ', ' Samsung')])
OrderedDict([('Model number', 'RW345'), ('Price', '46500'), (' Quantity', ' 14'), (' Company ', ' Onida')])
OrderedDict([('Model number', 'EX366'), ('Price', ' 30000'), (' Quantity', ' 8'), (' Company ', ' Lenovo')])
OrderedDict([('Model number', 'FU699'), ('Price', ' 45000'), (' Quantity', ' 12'), (' Company ', 'Acer')])
Example: Reading CSV File using CSV.DictReader() Function
The CSV file has initial spaces, quotes around each entry, and uses a delimiter. The csv.register_dialect()
function is used to define a custom dialect.
Syntax
csv.register_dialect(name[, dialect[, **fmtparams]])
The custom dialect requires a name in the form of a string. Other specifications can be done either by passing a sub-class of Dialect
class, or by individual formatting patterns.
import csv
csv.register_dialect('myDialect', delimiter='|', skipinitialspace=True, quoting=csv.QUOTE_ALL)
with open('model.csv', 'r') as csvfile:
reader = csv.reader(csvfile, dialect='myDialect')
for row in reader:
print(row)
['Model number', 'Price', 'Quantity', 'Company']
["ST001", '35000', '10', 'Samsung']
["RW345", '46500', '14', 'Onida']
["EX366", '30000', '8', 'Lenovo']
["FU699", '45000', '12', 'Acer']
While creating the reader object, we pass dialect='myDialect
' to specify that the reader instance must use that particular dialect. The advantage of using dialect is that it makes the program more modular.
Conclusion
In this article, we learned about CSV files and different ways to read a CSV file by using several built-in modules and libraries. We used Dialect
class, Dictionary Reader
object, and CSV Reader
object. We used some custom parsing codes as well to parse the CSV file using different text files and CSV files.