How Python Read CSV File into Array List | Explained With Example

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 a file format that is used to store the data in tabular format. This is the best format to store structured data.

It stored the data and separated data entries using delimiter comma (,). Many websites use 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 the 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.

For example:

Virat,45,43,Ind
Cook,25,27,Eng
Root,29,14,Eng

This is an example of cricket match info where the name of each player, runs they have scored, the 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 with any text file you can read and split the content using the 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.

import csv

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.

Output:

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 the 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[0], '|', row[3]

Output:

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 data scientists as they use it for reading and analyzing 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 a CSV file; you will find worthwhile information.

Kindly write in a comment if you have used a CSV file in your project or if you have done something interesting stuff with .csv files.

Happy Pythoning!

2 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *