Reading CSV files means accessing and processing data stored in a CSV (Comma-Separated Values) file. A CSV file stores tabular data in plain text format, where each line represents a row and values are separated using commas.
Note: For this article, we are using a csv file name data.csv, to download it click here.
Using csv.reader()
The csv.reader() function reads the CSV file line by line and returns each row as a list. It is useful when we want simple row-wise access to CSV data.
import csv
with open("data.csv", "r") as f:
data = csv.reader(f)
for row in data:
print(row)
Output

Explanation:
- open("data.csv", "r") opens the CSV file in read mode and csv.reader(f) reads the file row by row.
- Each row is returned as a list and for loop prints every row from the CSV file.
Using csv.DictReader()
The csv.DictReader() function reads the CSV file as dictionaries. The first row of the CSV file is treated as column headers.
import csv
with open("data.csv", "r") as f:
data = csv.DictReader(f)
for row in data:
print(row)
Output

Explanation:
- csv.DictReader(f) reads rows as dictionaries.
- CSV column names become dictionary keys and each row stores values corresponding to those keys.
Using pandas.read_csv()
The read_csv() function from the pandas library reads the CSV file and stores the data in a DataFrame. It provides a structured tabular format for working with data.
import pandas as pd
df = pd.read_csv("data.csv")
print(df)
Output

Explanation:
- pd.read_csv() reads the CSV file into a DataFrame.
- A DataFrame stores data in rows and columns.
- print(df) displays the CSV data in a table-like format.