How to read CSV file in Python
In this post, you will learn how to read a CSV file using the Python programming language.
In the wake of dealing with a dataset and doing all the preprocessing, we require to save the preprocessed information into some data format, like in CSV, Excel, or others. The CSV (Comma-separated-values) is widely used for data exchange in data-based applications. In web applications, we generally store the data in an array, dataframe, list, tuple, dictionary, etc. There may be a need for clients to export this data to a CSV file. The data in CSV is stored as sequences of records. Like in other programming languages, we can also read data from a CSV file in Python.
Here, we have mentioned two different ways to read the data from a CSV file in Python.
- CSV Module
- Pandas Library
Read CSV file in Python using the CSV module
The csv.reader() method is used to read the file. In the given Python program, first we open the file in reading mode using the open() function, which returns the file object, and then it is read by using the reader() method of the CSV module. It returns an iterable reader object. The reader object is then iterated using a for loop to print the contents of each row.
import csv
# opening the CSV file
with open('student_data.csv', mode ='r')as file:
# reading the CSV file
csvFile = csv.reader(file)
# displaying the contents of the CSV file
for lines in csvFile:
print(lines)
Output of the above code:
['Roll', 'Class', 'Firstname', 'Lastname', 'Section']
['1001', '10', 'Priska', 'Kashyap', 'A']
['1002', '10', 'John', 'Smith', 'B']
['1003', '8', 'Andrew', 'Roi', 'A']
['1004', '9', 'Abhiraj', 'Daav', 'A']
['1005', '8', 'Bhim', 'Raj', 'B']
Read CSV file in Python using Pandas library
Here, we have first imported the Pandas module and passed the CSV file as a parameter in the read_csv() method. This method reads the data into a Pandas DataFrame. It accepts a filename as a parameter. DataFrame is the key data structure of Pandas.
import pandas
# reading the CSV file
csvFile = pandas.read_csv('student_data.csv')
# displaying the contents of the CSV file
print(csvFile)
Output of the above code:
Roll Class Firstname Lastname Section
0 NaN NaN NaN NaN NaN
1 1001.0 10.0 Priska Kashyap A
2 1002.0 10.0 John Smith B
3 1003.0 8.0 Andrew Roi A
4 1004.0 9.0 Abhiraj Daav A
5 1005.0 8.0 Bhim Raj B
Related Articles
Convert JSON to CSV using Python
Convert Excel to CSV Python Pandas
Python Pandas Dataframe to CSV
Python Convert XML to CSV
Python Converting a CSV File to a MySQL Table
Python write to CSV file
Python read csv into list
Python Pandas CSV to Dataframe
Write list to CSV Python
Django Export Model Data to CSV
Convert Python list to numpy array
Remove element from list Python
Python dict inside list
Check if list is empty Python
Python iterate list with index
Python iterate list with index
Python split string by comma
Python Pandas DataFrame
Stemming and Lemmatization in Python
Python | Generate QR Code using pyqrcode module