Python csv: 14 flashcards

All 14 flashcards from this kit, free and with no sign-up.

1. What does the `csv` module provide for reading CSV files?

Show the answer

The `csv` module provides `csv.reader`, which returns an iterator that yields each row as a list of strings.

2. Why should you open files with `newline=''` when using the csv module?

Show the answer

To prevent extra blank lines from appearing in the output, especially on Windows, because the csv module handles line endings itself.

3. What is the purpose of `csv.DictReader`?

Show the answer

It reads CSV rows as dictionaries, using the header row as keys, so you can access columns by name.

4. How does the csv module handle fields that contain commas, quotes, or newlines?

Show the answer

It automatically wraps such fields in double quotes and escapes internal quotes by doubling them.

5. What is a dialect in the context of the csv module?

Show the answer

A dialect is a set of formatting parameters (like delimiter, quotechar, and quoting) that defines a specific CSV variation, which can be registered and reused.

6. How can you read a CSV from a string instead of a file?

Show the answer

Wrap the string with `io.StringIO` to create a file-like object, then pass it to `csv.reader`.

7. What data type does the csv module return for each field when reading?

Show the answer

All fields are returned as strings; you must explicitly convert to numbers or other types as needed.

8. What method do you use to write a single row with `csv.writer`?

Show the answer

`writerow()` takes an iterable of values and writes it as a single line in the CSV file.

9. How do you specify a semicolon as the delimiter when reading a CSV file?

Show the answer

Pass the `delimiter=';'` parameter to `csv.reader` (or `csv.writer`).

10. What is the main advantage of using the csv module over manual string splitting?

Show the answer

It correctly handles quoting and escaping of fields that contain commas, quotes, or newlines, which manual splitting would break.

11. What does `csv.writer` expect as the first argument?

Show the answer

A file object opened for writing (with `newline=''`), such as the result of `open('file.csv', 'w', newline='')`.

12. How does `csv.DictWriter` determine the column order when writing?

Show the answer

You must provide a `fieldnames` list that defines the order of columns; it uses that list to write the header and map dictionary keys.

13. What is the default quoting behavior of the csv module?

Show the answer

The default is `QUOTE_MINIMAL`, which quotes fields only when necessary (e.g., when they contain the delimiter, quotechar, or newline).

14. Why is the csv module considered a foundation for data processing?

Show the answer

It provides a reliable way to read and write tabular data, which is often the first step before using higher-level libraries like pandas.

Make your own

Your notes can be a kit like these

Every kit here was made from real study material. Point Veda at a chapter, a PDF, a photo or a link and get notes, flashcards, practice MCQs, games and a scored test — free, in 42 languages.

  • Chapter notes
  • Flashcards
  • Practice MCQs
  • Scored tests
  • Exam prep

Get Veda free See what students get

Free to start · No ads · Works in 42 languages