Build a CSV String
Intermediate · Write the code · from Python CSV: Read, Write, and Process CSV Files
From the lesson: Python CSV: Read, Write, and Process CSV Files
Think about a spreadsheet with rows and columns - names in one column, ages in another, scores in a third. A CSV file (Comma-Separated Values) is just a plain text version of that spreadsheet, where commas separate each column.
Read the lessonUse csv.writer to create a CSV string with a header row ['item', 'qty'] and two data rows: ['Apples', '5'] and ['Bananas', '3'].
Then seek back to the start and print the result with print(f.read(), end='').
import csv
import io
f = io.StringIO()
# Your code here: create writer, write header and rows, seek and print
More CSV exercises
- Read CSV and Print NamesBeginner
- Predict the DictReader OutputBeginner
- Fix the CSV Sum BugIntermediate
- Filter and Count CSV RowsAdvanced
All Python exercises · 50 practice problems · File I/O & Data Formats quiz