0
0
Pythonprogramming~30 mins

Working with CSV files in Python - Mini Project: Build & Apply

Choose your learning style9 modes available
Working with CSV files
📖 Scenario: You have a small store and keep track of your sales in a CSV file. You want to read this file, find sales above a certain amount, and show those sales.
🎯 Goal: Build a Python program that reads sales data from a CSV file, filters sales above a set amount, and prints those sales.
📋 What You'll Learn
Create a CSV file with sales data
Set a sales threshold value
Read the CSV file and filter sales above the threshold
Print the filtered sales
💡 Why This Matters
🌍 Real World
Many businesses store data in CSV files. Being able to read, filter, and process CSV data is useful for reports and analysis.
💼 Career
Data analysts, software developers, and anyone working with data often need to handle CSV files to extract useful information.
Progress0 / 4 steps
1
Create a CSV file with sales data
Create a CSV file named sales.csv with these exact contents including the header:
product,amount
Book,15
Pen,5
Notebook,20
Bag,50
Python
Need a hint?

Use the csv module and csv.writer to write rows to the file.

2
Set a sales threshold value
Create a variable called threshold and set it to 10 to filter sales above this amount.
Python
Need a hint?

Just create a variable named threshold and assign the number 10.

3
Read the CSV file and filter sales above the threshold
Read the sales.csv file using csv.DictReader. Create a list called filtered_sales that contains dictionaries for each sale where the amount is greater than threshold. Convert amount to an integer before comparing.
Python
Need a hint?

Use csv.DictReader to read rows as dictionaries. Check if int(row['amount']) is greater than threshold and add to filtered_sales.

4
Print the filtered sales
Use a for loop to print each dictionary in filtered_sales.
Python
Need a hint?

Use a for loop to print each dictionary in filtered_sales.