Recall & Review
beginner
What Python module is commonly used for reading and writing CSV files using dictionaries?
The
csv module provides DictReader and DictWriter classes to handle CSV files as dictionaries.Click to reveal answer
beginner
How does
csv.DictReader represent each row of a CSV file?csv.DictReader reads each row as a dictionary where keys are column headers and values are the row's data.Click to reveal answer
beginner
What method do you use to write a dictionary row to a CSV file using
csv.DictWriter?Use the
writerow() method of csv.DictWriter to write a single dictionary as a CSV row.Click to reveal answer
intermediate
Why is using dictionary-based CSV handling helpful compared to normal CSV reading?
It allows you to access data by column names instead of index positions, making code easier to read and less error-prone.
Click to reveal answer
beginner
What must you do before writing rows with
csv.DictWriter to ensure the CSV file has headers?Call the
writeheader() method once to write the column headers before writing any rows.Click to reveal answer
Which class reads CSV rows as dictionaries with column headers as keys?
✗ Incorrect
csv.DictReader reads CSV rows as dictionaries using headers as keys.What method writes the header row when using
csv.DictWriter?✗ Incorrect
The
writeheader() method writes the CSV header row.If you want to write a dictionary {'name': 'Anna', 'age': 30} as a CSV row, which method do you use?
✗ Incorrect
writerow() writes a single dictionary as a CSV row.What is the main advantage of using dictionary-based CSV handling?
✗ Incorrect
Using dictionaries lets you access data by column names, making code clearer.
Which of these is NOT a feature of
csv.DictReader?✗ Incorrect
csv.DictReader reads CSV files; it does not write them.Explain how to read a CSV file using dictionary-based handling in Python.
Think about how to open a file and use DictReader to get rows as dictionaries.
You got /4 concepts.
Describe the steps to write a list of dictionaries to a CSV file using csv.DictWriter.
Remember to write headers before rows.
You got /5 concepts.