0
0
Raspberry Piprogramming~20 mins

Logging to CSV files in Raspberry Pi - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
CSV Logging Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
1:30remaining
Logging sensor data to CSV: Output check
You run this command on your Raspberry Pi to append temperature data to a CSV file:

echo "$(date +%Y-%m-%d),$(date +%H:%M:%S),25.3" >> /home/pi/sensor_log.csv

What will be the last line added to sensor_log.csv?
Raspberry Pi
echo "$(date +%Y-%m-%d),$(date +%H:%M:%S),25.3" >> /home/pi/sensor_log.csv
AA line with temperature only, no date or time
BA line with current date and time but no temperature value
CAn error message about invalid syntax
DA line with current date, current time, and 25.3 separated by commas
Attempts:
2 left
💡 Hint
Think about what the echo command with date substitutions outputs.
Configuration
intermediate
2:00remaining
CSV logging script: Correct Python code
Which Python code snippet correctly appends a row with timestamp and sensor value to a CSV file on Raspberry Pi?
A
import csv
from datetime import datetime
with open('log.csv', 'a', newline='') as f:
  writer = csv.writer(f)
  writer.writerow([datetime.now(), 22.5])
B
import csv
with open('log.csv', 'w') as f:
  writer = csv.writer(f)
  writer.writerow(['time', 'value'])
C
import csv
from datetime import datetime
with open('log.csv', 'r') as f:
  writer = csv.writer(f)
  writer.writerow([datetime.now(), 22.5])
D
import csv
from datetime import datetime
with open('log.csv', 'a') as f:
  writer = csv.writer(f)
  writer.writerow(datetime.now(), 22.5)
Attempts:
2 left
💡 Hint
Appending means opening the file with 'a' mode and using csv.writer properly.
Troubleshoot
advanced
2:00remaining
CSV file has extra blank lines after logging
You wrote a Python script on Raspberry Pi to log data to CSV. After running it multiple times, the CSV file has extra blank lines between data rows. What is the most likely cause?
AThe csv.writer.writerow() was called with a string instead of a list
BThe file was opened with mode 'w' instead of 'a'
CThe file was opened without newline='' parameter in open()
DThe sensor data was not converted to string before writing
Attempts:
2 left
💡 Hint
Think about how Python handles newlines on different platforms when writing CSV.
🔀 Workflow
advanced
2:00remaining
Automating CSV logging with cron on Raspberry Pi
You want to log temperature data every 10 minutes to a CSV file automatically. Which cron job entry correctly runs a script located at /home/pi/log_temp.py every 10 minutes?
A*/10 * * * * /usr/bin/python3 /home/pi/log_temp.py >> /home/pi/log_temp.log 2>&1
B0 */10 * * * /usr/bin/python3 /home/pi/log_temp.py
C10 * * * * /home/pi/log_temp.py
D*/10 * * * * python /home/pi/log_temp.py
Attempts:
2 left
💡 Hint
Cron syntax for every 10 minutes uses */10 in the minutes field and full path to python.
Best Practice
expert
2:30remaining
Ensuring safe concurrent writes to CSV on Raspberry Pi
Multiple processes on your Raspberry Pi write sensor data to the same CSV file simultaneously. What is the best way to avoid data corruption?
AOpen the CSV file in 'w' mode to overwrite and avoid conflicts
BUse file locking mechanisms like flock to serialize writes
CWrite to separate CSV files and merge later manually
DUse print statements instead of csv.writer for simpler writes
Attempts:
2 left
💡 Hint
Think about how to prevent two processes writing at the same time.