How Python Read CSV File into Array List | Explained With Example
Want to learn how Python read CSV file into array list?
Why is it so popular data format for data science?
Before that let’s understand the format of the contents stored in a .csv file. And
What is a 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 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 the .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 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 Python Read CSV File into Array List?
As like any text file you can read and split the content using comma operator. But there is an 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 a CSV file in python?
First of all, you need to import the CSV module.
Open the file as a usual text file.
file_CSV = open(<CSV_file_name>)
The open() is a built-in function for file handling in Python.
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 File into Array 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 the 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 the names of countries without repetition by finding unique elements from the list in Python.
Like this, you can do a lot many things around CSV file contents.
Use of CSV file in Data Science Project
This is a simple tutorial to deal with CSV files. Hope you find it useful in your programming or in the project.
The CSV file is popular among the data scientist as they use it for reading and analyzing the data.
Here is a sample CSV file data you can download.
Now you know, How Python read CSV file into array list? 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.
Thank u for sharing