Editor

Filter a CSV Into a New File

#601 · File handling · Intermediate · 70 XP · about 12 min

Write filter_rows(source, target, column, minimum) that copies part of the CSV file source into a new CSV file target:

  • The header row (the first row) is always copied.
  • After it, copy every row whose value in the column named column is a whole number of at least minimum, keeping the rows in their order. A row whose cell in that column is empty is left out.
  • Replace whatever target held before.

Read and write with the csv module, so cells that contain commas stay in one piece. Return how many rows you copied, not counting the header.

Example

If results.csv holds:

name,class,marks
Asha,12A,88
Ben,12B,54
Cara,12A,91

then filter_rows('results.csv', 'passed.csv', 'marks', 60) returns 2, and passed.csv holds:

name,class,marks
Asha,12A,88
Cara,12A,91

Constraints

  • column is always one of the header's names.
  • The file has at most 10,000 rows and no blank lines.

Fastest solvers

Leaderboard

First solves only, ranked by stars and then time.