Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to open a CSV file for writing.
Raspberry Pi
file = open('data.csv', '[1]')
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using
'r' mode instead of 'w' mode.✗ Incorrect
To write data to a CSV file, you open it in 'w' (write) mode.
2fill in blank
mediumComplete the code to import the module needed to write CSV files.
Raspberry Pi
import [1]
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Importing unrelated modules like json or os.
✗ Incorrect
The csv module helps to write and read CSV files easily.
3fill in blank
hardFix the error in the code to write a row to the CSV file.
Raspberry Pi
writer = csv.writer(file) writer.[1](['time', 'temperature'])
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using non-existent methods like 'writeline' or 'readrow'.
✗ Incorrect
The correct method to write a single row is writerow.
4fill in blank
hardFill both blanks to open a CSV file and write a header row.
Raspberry Pi
with open('log.csv', [1], newline=[2]) as file: writer = csv.writer(file) writer.writerow(['date', 'value'])
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using read mode
'r' or setting newline to True.✗ Incorrect
Open the file in write mode ('w') and set newline to '' to avoid extra blank lines on Windows.
5fill in blank
hardFill all three blanks to write multiple rows to a CSV file using a loop.
Raspberry Pi
with open('data.csv', [1], newline=[2]) as file: writer = csv.writer(file) for [3] in range(3): writer.writerow([f'Day {i+1}', i*10])
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using append mode instead of write, omitting newline=
'' causing blank lines, or wrong loop variable.✗ Incorrect
Open the file in write mode ('w'), set newline to '', and use 'i' as the loop variable.