0
0
PythonHow-ToBeginner · 3 min read

How to Read CSV File in Python: Simple Guide

To read a CSV file in Python, you can use the built-in csv module or the popular pandas library. The csv.reader() function reads the file line by line, while pandas.read_csv() loads the file into a table-like DataFrame for easy data handling.
📐

Syntax

Here are two common ways to read CSV files in Python:

  • Using csv module: Open the file and pass it to csv.reader() to iterate over rows.
  • Using pandas library: Use pandas.read_csv() to load the CSV into a DataFrame object.
python
import csv

with open('file.csv', mode='r', newline='') as file:
    reader = csv.reader(file)
    for row in reader:
        print(row)

# Using pandas
import pandas as pd
df = pd.read_csv('file.csv')
print(df)
💻

Example

This example shows how to read a CSV file named data.csv using both the csv module and pandas. It prints each row with csv and prints the whole table with pandas.

python
import csv
import pandas as pd

# Using csv module
print('Reading with csv module:')
with open('data.csv', mode='r', newline='') as file:
    reader = csv.reader(file)
    for row in reader:
        print(row)

# Using pandas
print('\nReading with pandas:')
df = pd.read_csv('data.csv')
print(df)
Output
Reading with csv module: ['Name', 'Age', 'City'] ['Alice', '30', 'New York'] ['Bob', '25', 'Los Angeles'] ['Charlie', '35', 'Chicago'] Reading with pandas: Name Age City 0 Alice 30 New York 1 Bob 25 Los Angeles 2 Charlie 35 Chicago
⚠️

Common Pitfalls

Common mistakes when reading CSV files include:

  • Not specifying newline='' when opening files with csv.reader(), which can cause extra blank lines on some systems.
  • Forgetting to install or import pandas before using read_csv().
  • Assuming all CSV files have the same delimiter; some use semicolons or tabs instead of commas.
  • Not handling encoding issues, which can cause errors if the file contains special characters.
python
import csv

# Wrong way: missing newline='' can cause blank lines
with open('data.csv', 'r') as file:
    reader = csv.reader(file)
    for row in reader:
        print(row)

# Right way:
with open('data.csv', 'r', newline='') as file:
    reader = csv.reader(file)
    for row in reader:
        print(row)
📊

Quick Reference

Summary tips for reading CSV files in Python:

  • Use csv.reader() for simple row-by-row reading.
  • Use pandas.read_csv() for powerful data analysis and easy data manipulation.
  • Always open files with newline='' when using csv module.
  • Check the delimiter and encoding of your CSV file if you get errors or wrong data.

Key Takeaways

Use the built-in csv module or pandas library to read CSV files in Python.
Open files with newline='' when using csv.reader() to avoid blank lines.
pandas.read_csv() loads CSV data into a DataFrame for easy data handling.
Check your CSV file's delimiter and encoding to avoid errors.
Install pandas with pip if you want to use its powerful CSV reading features.