Module 5 Lesson 3: Working with CSV Data
Process spreadsheets like a pro. Learn how to use Python's built-in csv module to read and write structured data for analysis.
Module 5 Lesson 3: Working with CSV Data
CSV stands for Comma Separated Values. It’s the world's most popular format for spreadsheets and simple databases. In this lesson, we’ll learn how to use Python's built-in csv module to handle these files with ease.
Lesson Overview
In this lesson, we will cover:
- What is a CSV?: Understanding the structure.
- The
csv.reader: Reading data as lists. - The
csv.DictReader: Reading data as dictionaries (Highly Recommended!). - Writing CSVs: Saving your data as a spreadsheet.
1. What is a CSV?
A CSV file is just a text file where each line is a row, and each value is separated by a comma.
Example students.csv:
name,age,grade
Alex,15,A
Sara,16,B+
2. Reading with DictReader
While you can read a CSV as a list, it’s much safer to read it as a Dictionary. This way, you can access data using the header names rather than worrying about column numbers.
import csv
with open("students.csv", "r") as file:
reader = csv.DictReader(file)
for row in reader:
print(f"{row['name']} is in {row['grade']} grade.")
Visualizing the Process
graph TD
Start[Input] --> Process[Processing]
Process --> Decision{Check}
Decision -->|Success| End[Complete]
Decision -->|Retry| Process
3. Writing to a CSV
To save data as a CSV, we use csv.writer or csv.DictWriter.
import csv
data = [
{"name": "John", "score": 90},
{"name": "Maya", "score": 95}
]
with open("scores.csv", "w", newline="") as file:
headers = ["name", "score"]
writer = csv.DictWriter(file, fieldnames=headers)
writer.writeheader() # Write the top row
writer.writerows(data) # Write all the rows at once
(Note: newline="" prevents extra blank lines from appearing in some operating systems!)
4. Why Use CSV?
- Excel Compatible: You can open any CSV in Microsoft Excel or Google Sheets.
- Lightweight: It’s much smaller than a
.xlsxfile. - Standard: Almost every data science tool in the world understands CSVs.
Practice Exercise: The Inventory Manager
- Create a CSV file named
inventory.csvwith 3 columns:item,quantity,price. - Write a Python script that reads this file.
- Calculate the Total Value of the inventory (quantity * price).
- Print a summary:
"Total Inventory Value: $X.XX".
Quick Knowledge Check
- What does CSV stand for?
- Which is easier to read:
csv.reader(lists) orcsv.DictReader(dictionaries)? Why? - What is the purpose of
writer.writeheader()? - Can you open a CSV file in a normal text editor like Notepad or VS Code?
Key Takeaways
- CSVs are plain text spreadsheets separated by commas.
DictReaderandDictWriterare the professional way to handle CSVs.- Always use
newline=""when opening a file for CSV writing. - CSVs are the bridge between your code and tools like Excel.
What’s Next?
CSVs are great for tables, but what if your data is more complex (like a user with multiple addresses and a list of friends)? In Lesson 4, we’ll learn about JSON Files—the gold standard for web data!