0
0
Raspberry Piprogramming~30 mins

Logging to CSV files in Raspberry Pi - Mini Project: Build & Apply

Choose your learning style9 modes available
Logging to CSV files
📖 Scenario: You have a Raspberry Pi that collects temperature readings every hour. You want to save these readings in a CSV file so you can analyze them later.
🎯 Goal: Build a simple Python program that logs temperature readings with timestamps into a CSV file.
📋 What You'll Learn
Create a list of temperature readings with timestamps
Set a CSV filename variable
Write the temperature data to the CSV file with headers
Print a confirmation message after writing
💡 Why This Matters
🌍 Real World
Logging sensor data like temperature on a Raspberry Pi is common in home automation and environmental monitoring projects.
💼 Career
Understanding how to log data to CSV files is a basic skill for DevOps and IoT engineers who manage device data and automate reports.
Progress0 / 4 steps
1
Create temperature data list
Create a list called temperature_data with these exact entries: ("2024-06-01 10:00", 22.5), ("2024-06-01 11:00", 23.0), ("2024-06-01 12:00", 23.3).
Raspberry Pi
Need a hint?

Use a list of tuples where each tuple has a timestamp string and a temperature float.

2
Set CSV filename
Create a variable called csv_filename and set it to the string "temperature_log.csv".
Raspberry Pi
Need a hint?

Use a string variable to store the filename.

3
Write data to CSV file
Import the csv module. Use open(csv_filename, 'w', newline='') to open the file for writing. Create a csv.writer object called writer. Write a header row with ["Timestamp", "Temperature"]. Then write all rows from temperature_data using a for loop with variables timestamp and temp.
Raspberry Pi
Need a hint?

Use the csv module to write rows. Remember to write the header first.

4
Print confirmation message
Add a print statement after writing the CSV file that outputs exactly: "Data logged to temperature_log.csv".
Raspberry Pi
Need a hint?

Use print() to show the message after the file is written.