Command Palette

Search for a command to run...

Hectal
Phase 7: Errors, Files and Data Formats
Python · Exercise 4 of 7 PreviousNext
Intermediate

Total a CSV column

Write total_by_item(text) for CSV text with the header item,qty. Return a dict mapping each item to the sum of its qty values (as integers).

Rows whose qty is not a whole number (for example x) must be skipped. Use csv.DictReader and io.StringIO (it lets DictReader read from a string as if it were a file). Hint: all values from CSV are text.

What Check tests

  1. Sums repeated items
  2. Skips bad quantities
  3. Quoted commas stay inside the value
  4. Only a header gives an empty dict
  5. Blank and decimal quantities are skipped
  6. An item with only bad rows is not listed

Stuck? One hint at a time

The solution unlocks after your first Check. Try first: that is where the learning happens.

main.py
Sign in to run your code.

Output

Press Run to see what your program prints.