CSV files are everywhere. From sales reports and contact lists to machine learning datasets—Comma-Separated Values (CSV) format is one of the simplest and most widely used file formats for data exchange.
If you're learning Python, knowing how to read a CSV file in Python is essential. Whether you’re a student, data analyst, or developer, this skill opens doors to real-world data handling.

In this blog, we’ll walk you through:
CSV stands for Comma-Separated Values. It’s a plain text file where each line represents a data record, and each value is separated by a comma.
Sample CSV content:
pgsql
CopyEdit
Copy Code
Name,Age,City Ankit,25,Delhi Priya,30,Mumbai John,28,Bangalore
Here, the first line is the header, and the following lines are the data rows.
Python has a built-in module called csv to work with CSV files.
📘 Basic Example
python
CopyEdit
import csv
with open('people.csv', mode='r') as file:
reader = csv.reader(file)
for row in reader:
print(row)
✅ Output:
css
CopyEdit
Copy Code
['Name', 'Age', 'City'] ['Ankit', '25', 'Delhi'] ['Priya', '30', 'Mumbai'] ['John', '28', 'Bangalore']
🔍 Explanation:
If you prefer to work with column names, DictReader is your friend.
python
CopyEdit
import csv
with open('people.csv', mode='r') as file:
reader = csv.DictReader(file)
for row in reader:
print(row['Name'], row['Age'], row['City'])
✅ Output:
nginx
CopyEdit
Copy Code
Ankit 25 Delhi Priya 30 Mumbai John 28 Bangalore
This method treats the first row as a header and returns each row as a dictionary, making it easy to access fields by name.
The pandas library is a powerful tool for data manipulation and analysis. It makes reading CSV files a breeze.
🚀 Example:
python
CopyEdit
import pandas as pd
df = pd.read_csv('people.csv')
print(df)
✅ Output:
markdown
CopyEdit
Copy Code
Name Age City 0 Ankit 25 Delhi 1 Priya 30 Mumbai 2 John 28 Bangalore
With pandas, you can even read a CSV file from the internet:
python
CopyEdit
import pandas as pd
Copy Code
url = 'https://people.sc.fsu.edu/~jburkardt/data/csv/airtravel.csv' df = pd.read_csv(url) print(df.head())
This is great when working with open datasets or APIs.
Sometimes, the values are separated by a character other than a comma, like a semicolon (;) or tab (\t).
python
CopyEdit
Copy Code
import pandas as pd
df = pd.read_csv('data_semicolon.csv', delimiter=';')
print(df)This tells Python what to use as the separator between values.
If you encounter errors like:
vbnet
CopyEdit
UnicodeDecodeError: 'utf-8' codec can't decode byte
Try specifying the encoding:
python
CopyEdit
df = pd.read_csv('file.csv', encoding='ISO-8859-1')
Common encodings: utf-8, ISO-8859-1, latin1
Once you know how to read a CSV, you’ll also want to write one!
python
CopyEdit
import csv
Copy Code
data = [['Name', 'Age'], ['Ravi', 22], ['Kiran', 24]]
with open('output.csv', mode='w', newline='') as file:
writer = csv.writer(file)
writer.writerows(data)This will create a new file named output.csv with your data.
| Error Message | Cause | Solution |
| FileNotFoundError | File path is incorrect or missing | Double-check file location |
| UnicodeDecodeError | Wrong encoding | Use encoding='latin1' or similar |
| ParserError in pandas | Malformed file | Check file content & separator |
| All data in one column (in pandas) | Wrong delimiter | Use delimiter or sep parameter |
✅ Always close the file or use with open() (auto-closes it)
✅ Use csv.DictReader for field-level access
✅ Use pandas if working with large or complex datasets
✅ Always check file encoding when reading from unknown sources
✅ Validate your CSV structure before processing
| Use Case | Application |
| Data Science & ML | Load datasets for training/testing |
| Financial Analysis | Read stock market or sales reports |
| Web Scraping | Store scraped data in CSV format |
| Reporting Tools | Generate CSV reports from applications |
| Contact Management | Export/import contacts |
If you're excited to explore data science, automation, or software development, mastering file handling and CSV in Python is just the beginning.
Take the next step by joining Uncodemy’s Python Programming Course in Noida.
✅ Python from basics to advanced
✅ File handling, APIs, CSV, JSON, web scraping
✅ Assignments, projects, and interview prep
✅ Certificate + placement support
✅ Suitable for students, working professionals, and beginners
Understanding how to read CSV file in Python equips you to handle real-world data with ease. Whether you’re building a chatbot, a dashboard, or a data science model, knowing how to read, manipulate, and analyze CSV data is an essential skill.
So go ahead—try reading your own CSV file, play around with pandas, and start building something amazing. And if you’re ready to take your Python skills further, don’t forget to check out Uncodemy’s expert-led Python course in Noida.
Personalized learning paths with interactive materials and progress tracking for optimal learning experience.
Explore LMSCreate professional, ATS-optimized resumes tailored for tech roles with intelligent suggestions.
Build ResumeDetailed analysis of how your resume performs in Applicant Tracking Systems with actionable insights.
Check ResumeAI analyzes your code for efficiency, best practices, and bugs with instant feedback.
Try Code ReviewPractice coding in 20+ languages with our cloud-based compiler that works on any device.
Start Coding
TRENDING
BESTSELLER
BESTSELLER
TRENDING
HOT
BESTSELLER
HOT
BESTSELLER
BESTSELLER
HOT
POPULAR