How to Read a CSV File Into Array in Python: A Step-by-Step Guide

Share This Post

Do you need to read a CSV file in Python? Are you struggling to figure out how to do it? If so, don’t worry, you’re not alone. The process of reading a CSV file can be confusing for beginners. In this step-by-step guide, we will show you how to read a CSV file in Python. We will also explain what a CSV file is and discuss some of the benefits of using this format.

The Python code to import a CSV file and split it up into an array was as follows.

This will give us the ability to be able to read data quickly while still having full control over how we want our variables organized.

Most of the time Python reads CSV files into an 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.

Python read CSV file into an array

Reading a CSV file in Python is actually pretty simple. You just need to use the built-in Python csv module. This module provides a number of functions that allow you to read and write CSV files. In this guide, we will focus on the reader() function. This function allows you to read a CSV file and store it.

So you may get what does it really is.

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.

To use the reader() function, you need to pass it to a file object. This can be either a path to a file on your computer. Alternatively, you can use a Python file object. If you are reading from a file on your computer, you will need to specify the path to the

Import CSV

Python read CSV file into array

Open the file as a usual text file.


file_CSV = open('cricket_Info.csv')

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('cricket_Info.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 a .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('cricket_Info.csv')

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

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

How to create a CSV file into an array

If you want to create a CSV file using the following info below without any error just copy and paste the code.


import csv  

data =[['Virat', '45', '43', 'Ind'], ['Cook', '25', '27', 'Eng'],
['Root', '29', '14', 'Eng']]

with open('cricket_Info.csv', 'w', encoding='UTF8') as f:
    writer = csv.writer(f)
    print("Your file the cricket_Info successfully done.")
    writer.writerow(data)

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

import csv


file_CSV = open('cricket_Info.csv')
data_CSV = csv.reader(file_CSV)
list_CSV = list(data_CSV)
for row in list_CSV:
	for now in row:
		print(now)

Output

[‘Virat’, ’45’, ’43’, ‘Ind’],
[‘Cook’, ’25’, ’27’, ‘Eng’],
[‘Root’, ’29’, ’14’, ‘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 index error: list assignment index out of range

spot_img

Related Posts

Python for Loop Iteration: How to Easily Manipulate a List

One of the most powerful features of the Python...

How To Get First Characters of a String in Python

Way to find the first characters of a string...

How To Convert Tuple To String In Python

In this post, you will learn how to convert...

How To Convert String to Double in Python

Good to hear, that you want to convert a...

Python TypeError: String Index Out Of Range Solution

A string is an array of characters and part...

How To Remove Empty Strings From a List Of Strings

On this page, we will discuss how to remove...
- Advertisement -spot_img