Processing CSV Files - Python

 

Introduction

Have you ever wondered how Excel files work behind the scenes? 📊 Welcome to the world of CSV files! With Python, we can read, write, and manipulate CSV files easily—without needing to open a spreadsheet! 

1. What is a CSV File? 

CSV (Comma-Separated Values) files store tabular data in plain text. Each row represents a record, and columns are separated by commas.

Example CSV content:

Name, Age, Profession
Alice, 25, Developer
Bob, 30, Designer
Charlie, 28, Data Scientist

2. Reading CSV Files 

Let’s use Python’s built-in csv module to read a CSV file!

import csv

with open("data.csv", mode="r") as file:
    reader = csv.reader(file)
    for row in reader:
        print(row)  # Each row is a list!

This prints:

['Name', 'Age', 'Profession']
['Alice', '25', 'Developer']
['Bob', '30', 'Designer']
['Charlie', '28', 'Data Scientist']

Pro Tip: Use csv.DictReader() to read CSV files as dictionaries for better readability!

with open("data.csv", mode="r") as file:
    reader = csv.DictReader(file)
    for row in reader:
        print(row["Name"], "is a", row["Profession"])

3. Writing to CSV Files 

Want to add new data? Use csv.writer()!

with open("data.csv", mode="a", newline="") as file:
    writer = csv.writer(file)
    writer.writerow(["David", 35, "Engineer"])

 This adds:

David, 35, Engineer

Tip: Use writerows() to write multiple rows at once!

4. Editing CSV Files

Python can also modify existing CSV files using pandas! 🐼

import pandas as pd

df = pd.read_csv("data.csv")
df.loc[df["Name"] == "Alice", "Age"] = 26

df.to_csv("data.csv", index=False)

This updates Alice’s age from 25 to 26!

5. Bonus: Handling Large CSV Files

If you’re working with huge CSV files (millions of rows), use pandaschunksize for efficient reading:

for chunk in pd.read_csv("big_data.csv", chunksize=1000):
    print(chunk.head())

This reads 1,000 rows at a time, making it memory-efficient!

Conclusion

With Python, handling CSV files is easy and fun!

Read CSV files,  Write & update CSV data Use pandas for advanced operations Handle large CSV files efficiently.


Happy coding!

Post a Comment

0 Comments