Python read CSV file into array?

Spread the love

Most of the time Python read CSV file into array CSV represents Comma Separated Variable.

It is a document design that is utilized to store the information in tabular format.

This is the best arrangement to store organized information.

So you may get what does it really.

It put away the data and separated data sections utilizing delimiter comma (,).

Numerous sites utilized CSV record design for sharing valuable data.

As with any content document you can peruse and part the content utilizing a comma operator.

However, there are automated modules called CSV.

Simply import it and it will do the things for you.

First of all, you need to import the CSV module.

Import CSV

Python read CSV file into array

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.

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 and run it.



[[‘Virat’, ‘45’, ‘43’, ‘Ind’], [‘Cook’, ‘25’, ‘27’, ‘Eng’], [‘Root’, ‘29’, ‘14’, ‘Eng’]]

If you don’t have a .csv file in your current directory, you will get the following error.

Traceback (most recent call last):

File “”, line 3, in

File_CSV = open(‘crickInfo.csv’)

IOError: [Errno 2] No such file or directory: ‘crickInfo.csv’

So just ensure you have a .csv file in the current directory where the actual .py file is stored.

Manipulating CSV file using Python program

Presently you can control and play with the information.

Print the name of the player and the group he has a place.

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]


Virat | Ind

Cook | Eng

Root | Eng

we can see there are some repeated countries.

I can find all the names of countries without repetition by finding unique elements from the list in Python.

You can do a lot many things around CSV file contents.

Read More: Python indexerror: list assignment index out of range

Leave a Comment