Python Program to read CSV file into List | Explained With Example
Before writing code for How to read CSV file in Python. You must know contents format stored in .csv file.
What is CSV file?
CSV stands for Comma Separated Variable. It is file format which is used to store the data in tabular format. This is the best format to store the structured data.
So you might get what does it actually. It stored the data and separated data entries using delimiter comma (,). Many websites used CSV file format for sharing valuable data.
One of the primary examples of CSV files is storing contact information. You may have used CSV files format for import and export the contact data files.
At the end of this post, I will mention CSV file link from where you can download .csv file. You can use this file for practicing, parsing and manipulating data.
As of now for simplicity, consider this simple CSV file containing cricket related data.
Virat,45,43,Ind Cook,25,27,Eng Root,29,14,Eng
This is the example of cricket match info where the name of the each player, run they have scored, balls they have faced and the team they belong stored in CSV files.
Save this file as “crickInfo.csv”.
How to read CSV file in python?
As like any text file you can read and split the content using comma operator. But there is automated module called csv. Just import it and it will do the things for you.
In this post, I will show you how to read CSV file in python?
First of all, you need to import CSV module.
Open the file as usual text file.
file_CSV = open(<CSV_file_name>)
Then we need CSV.reader() to get structured data from .csv files.
data_CSV = csv.reader(file_CSV)
A list is the most used and convenient data structure in python so converting csv files data into a list makes the data manipulation easy.
list_CSV = list(data_CSV)
Python Read csv into list:
import csv file_CSV = open('crickInfo.csv') data_CSV = csv.reader(file_CSV) list_CSV = list(data_CSV) print list_CSV
Save this program as readCSVFile.py and run it.
python readCSVFile.py [['Virat', '45', '43', 'Ind'], ['Cook', '25', '27', 'Eng'], ['Root', '29', '14', 'Eng']]
If you don’t have .csv file in your current directory, you will get following error.
Traceback (most recent call last): File "readCSVFile.py", line 3, in file_CSV = open('crickInfo.csv') IOError: [Errno 2] No such file or directory: 'crickInfo.csv'
So just ensure you have .csv file in the current directory where the actual .py file is stored.
Manipulating CSV file data using Python Program:
Now you can manipulate and play with the data.
You might be curious.
Print name of the player and the team he belongs.
import csv file_CSV = open('crickInfo.csv') data_CSV = csv.reader(file_CSV) list_CSV = list(data_CSV) for row in list_CSV: print row, '|', row
Virat | Ind Cook | Eng Root | Eng
You can see there are some repeated countries. You can find all names of countries without repetition by finding unique elements from list in Python.
Like this, you can do a lot many things around CSV file contents.
CSV file to download and Analyse with Python:
This is a simple tutorial to deal with CSV files. Hope you find it useful in your programming or in the project.
Here is a sample CSV file data you can download.
Now you know, How to read CSV file in python? So use this code and analyze contents in CSV file; you will find really worth information.
Kindly write in a comment if you have used CSV file in your project or you have done something interesting stuff with .csv files.
Happy Python Coding!