0
0
Data Analysis Pythondata~30 mins

Exporting to CSV in Data Analysis Python - Mini Project: Build & Apply

Choose your learning style9 modes available
Exporting to CSV
📖 Scenario: You work in a small bakery. You have a list of daily sales data for different types of bread. You want to save this data into a CSV file so you can share it with your manager easily.
🎯 Goal: Create a Python program that stores the bakery sales data in a dictionary, then export this data to a CSV file named sales.csv.
📋 What You'll Learn
Create a dictionary called sales with bread types as keys and number of items sold as values.
Create a list called headers with two strings: 'Bread' and 'Quantity'.
Use the csv module to write the dictionary data into a CSV file named sales.csv.
The CSV file should have a header row with the column names from headers.
💡 Why This Matters
🌍 Real World
Exporting data to CSV files is common in business to share reports, sales data, or any tabular information with others who may use spreadsheet software.
💼 Career
Data analysts and scientists often export processed data to CSV files for reporting, sharing with teams, or further analysis in other tools.
Progress0 / 4 steps
1
Create the sales data dictionary
Create a dictionary called sales with these exact entries: 'Baguette': 30, 'Sourdough': 20, 'Rye': 15, 'Focaccia': 10.
Data Analysis Python
Need a hint?

Use curly braces {} to create a dictionary with keys and values separated by colons.

2
Create the CSV header list
Create a list called headers with two strings: 'Bread' and 'Quantity'.
Data Analysis Python
Need a hint?

Use square brackets [] to create a list with the two column names as strings.

3
Write the sales data to a CSV file
Import the csv module. Open a file named sales.csv in write mode as file. Create a csv.writer object called writer using file. Write the headers list as the first row. Then, use a for loop with variables bread and quantity to iterate over sales.items() and write each bread and quantity as a row using writer.writerow.
Data Analysis Python
Need a hint?

Use csv.writer to write rows. Remember to write the header first, then loop through the dictionary items to write each row.

4
Print confirmation message
Write a print statement that outputs exactly: "Data exported to sales.csv successfully!".
Data Analysis Python
Need a hint?

Use print() with the exact message inside quotes.