Python read csv into list
In this post, you will learn how to read a CSV file into a list.
CSV file stands for "Comma-separated-values", as it uses comma to separate values. This is a widely used file format that stores data in a tabular format. The data in a CSV are stored as sequences of records.
A list is a sequence of indexed and ordered values like an array. It is mutable, means we can change the order of elements in a list. A list in Python is a linear data structure that can hold heterogeneous elements. It is flexible to shrink and grow and there is no need to declare it.
With the help of Python, we can easily read data from a CSV file and stored into a list.
Using Pandas
In the given Python code, we have used the pandas library to read the CSV file into a list. Pandas is open-source, fast, flexible, powerful and easy-to-use tool for data manipulation and data analysis, built on top of the Python programming language. It provides rich data structures and functions to make data analysis easy and fast.
Pandas library provides a function named tolist() that converts the data into a list. In the given example, we have used the read_csv() function to read the CSV file and then used the tolist() method to convert it into a list.
import pandas as pd
# reading CSV file
data = pd.read_csv('student_file.csv', delimiter=',')
# Iterating over the data
list_of_rows = [list(row) for row in data.values]
# printing list data
print(list_of_rows)
Output of the above code:
[["{'Roll': 1001, 'class': 10, 'firstname': 'Priska', 'lastname': 'Kashyap', 'section': 'A'}"],
["{'Roll': 1002, 'class': 10, 'firstname': 'John', 'lastname': 'Smith', 'section': 'B'}"],
["{'Roll': 1003, 'class': 10, 'firstname': 'Aditya', 'lastname': 'Roi', 'section': 'A'}"]]
Using csv module
Python provides an inbuilt csv module that contains a reader() method. In the given example, we have imported the csv module and opened the csv file using the open() method. It returns a file object that has been opened for reading the csv file. Next, we have called the csv.reader(file) with the file object as a file to return a csv reader object. Iterate over this csv line reader and append the data to the list.
import csv
# open csv file
file = open("company.csv", "r")
# reading csv file
csv_reader = csv.reader(file)
# Initialize empty list
lists_from_csv = []
for row in csv_reader:
lists_from_csv.append(row)
# printing data of csv
print(lists_from_csv)
Output of the above code:
[['', 'Name', 'Phone', 'Email', 'Date'],
['0', 'Alaya', '3290349906', This email address is being protected from spambots. You need JavaScript enabled to view it.', '02-03-2019 11:16'],
['1', 'Carle', '9059098968', This email address is being protected from spambots. You need JavaScript enabled to view it.', '01-06-2019 10:06'],
['2', 'Amma', '6750390948', This email address is being protected from spambots. You need JavaScript enabled to view it.', '05-04-2019 16:30']]
Related Articles
Python Convert XML to CSV
Python Pandas Dataframe to CSV
Python Converting a CSV File to a MySQL Table
Convert Excel to CSV Python Pandas
Generate and download a CSV file in Django
Python write to csv file
Python Pandas Dataframe to CSV
Python Pandas CSV to Dataframe
Prettytable in Python
Python dict inside list
Convert dictionary to dataframe Python
Convert List to Dataframe Python
Python add list to list
Difference between tuple and list in Python
Convert Excel to CSV Python Pandas
Alphabetical order Python
Python | Generate QR Code using pyqrcode module