How to Read CSV into a List in Python
This article explains how to read a CSV into a list in Python.
Suppose we have a CSV
file Employees.csv
with the following content.
Id | Name | Department | Salary | |
---|---|---|---|---|
1 | Sam | Human Resource | sam@gmail.com | 65K |
2 | John | Management | john@gmail.com | 58K |
3 | Tony | IT | tony@gmail.com | 70K |
4 | Mike | Accounts | mike@gmail.com | 35K |
If you open this file with a text editor, its contents should look like this.
Id,Name,Department,email,Salary
1,Sam,Human Resource,sam@gmail.com,65K
2,John,Management,john@gmail.com,58K
3,Tonny,IT,tonny@gmail.com,70K
4,Mike,Accounts,mike@gmail.com,35K
Now we will import the above data from this CSV file into a list in Python.
csv.reader
Read CSV into a list in Python using
Python has a built-in CSV
module called which has a reader class to read the contents of a CSV file. The sample code to read CSV to list in Python is as follows.
from csv import reader
with open("Employees.csv", "r") as csv_file:
csv_reader = reader(csv_file)
# Passing the cav_reader object to list() to get a list of lists
list_of_rows = list(csv_reader)
print(list_of_rows)
csv_reader = reader(csv_file)
Pass the file object csv_file
to csv.reader()
the function and get reader
the object. It returns an iterator to iterate over all the rows of the CSV file.
list_of_rows = list(csv_reader)
Converts csv.reader
a object to a list where each element in the list represents a row of the CSV and each item in the list represents a cell or column in a row.
Output:
[['Id', 'Name', 'Company', 'email', 'Salary'],
['1', 'Sam', 'Human Resource', 'sam@gmail.com', '65K'],
['2', 'John', 'Management', 'john@gmail.com', '58K'],
['3', 'Tonny', 'IT', 'tonny@gmail.com', '70K'],
['4', 'Mike', 'Accounts', 'mike@gmail.com', '35K']]
csv.reader
Read CSV into a list in Python using and other delimiters
csv.reader
The function also provides an option to read text files where the values are separated by other characters than commas. For example, the delimiter can be a tab or a space. To read such a file, we need to pass an additional parameter delimiter
to the reader function. See the following example.
If we have a file Employees_TSV.csv
with the same content as in Employees.csv, but separated by tabs instead of commas.
from csv import reader
with open("Employees_TSV.csv", "r") as csv_file:
csv_reader = reader(csv_file, delimiter="\t")
list_of_rows = list(csv_reader)
print(list_of_rows)
In the above code we are reading data from a file of values separated by tabs. delimiter = '\t'
The delimiter in the specified CSV file is the tab character.
The output of this code is the same as above.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Convert a list to a set in Python
Publish Date:2025/05/08 Views:144 Category:Python
-
This tutorial demonstrates how to convert a list to a set in Python. Difference between List and Set Lists and sets are standard Python data types that store values in sequence. Python list stores comma separated values between
Remove the first element from a list in Python
Publish Date:2025/05/08 Views:172 Category:Python
-
This tutorial will discuss different ways on how to remove the first element from a list. pop() Remove the first element from a list using the pop() Method can remove an element from a specific index. We have to specify the index from which
Convert a list to lowercase in Python
Publish Date:2025/05/08 Views:112 Category:Python
-
Lists can be used to store multiple items in a single variable. In Python, we can create a list of strings by enclosing the different elements in the list in single or double quotes. This tutorial demonstrates how to convert a list of strin
Remove all occurrences of an element from a Python list
Publish Date:2025/05/08 Views:69 Category:Python
-
In Python, lists allow the same element to appear multiple times. Even though the value of an element may be the same as other elements, each element will have a different index. Using these index numbers, you can easily access any element
Convert hex to bytes in Python
Publish Date:2025/05/08 Views:101 Category:Python
-
Hexadecimal, often abbreviated to hex, uses 16 symbols (0-9, a-f) to represent values, in contrast to the decimal system's 10. For example, 1000 in decimal is 3E8 in hexadecimal. Being proficient in dealing with hexadecimal is essential for
b in front of string in Python
Publish Date:2025/05/08 Views:53 Category:Python
-
This tutorial will discuss the statement in Python b" . b" Using the statement in Python b" The notation is used to specify strings in Python bytes . In contrast to regular strings with ASCII characters, bytes a string is an array of byte v
How to Convert Integer to Binary in Python
Publish Date:2025/05/08 Views:130 Category:Python
-
This tutorial explains how to convert an integer to binary in Python. This tutorial also lists some sample codes to illustrate different ways of converting from int to binary in Python. bin() Convert Int to Binary in Python using In Python,
How to convert bytes to int in Python
Publish Date:2025/05/08 Views:111 Category:Python
-
Bytes The data type has a numerical range of 0~255 (0x00~0xFF). A byte has 8 bits of data, that's why its maximum value is 0xFF. In some cases, you need to convert a byte or byte array to an integer for further data processing. Let's see ho
How to Remove Punctuation from a String in Python
Publish Date:2025/05/08 Views:190 Category:Python
-
This tutorial discussed methods for removing punctuation from strings in Python. This is a particularly useful step when preprocessing and cleaning text data for NLP. string Remove punctuation from a string in Python using class methods We