This is an archived version of the course. Please find the latest version of the course on the main webpage.

Chapter 7: More file handling

Exercise

face Josiah Wang

Here’s a CSV reading exercise.

Download the file grades.csv. This file was taken from John Burkardt’s CSV collection.

Open up the file in a plain text editor. Examine it. This is quite a ‘noisy’ CSV file.

  • Note that CSV files sometimes enclose strings in quotes (but not always). csv.reader will strip off the quotes for you in the background!
  • Note the irregular spacing between the columns. csv.reader does not handle these well out of the box. You should specify skipinitialspace=True to csv.reader() to automatically handle these pesky spaces!

Task

Your task is to use the csv module to read the CSV file. Then print out each student’s first name, last name, and the final mark and grade.

Expected output

Aloysius Alfalfa: 49.0 (D-)
University Alfred: 48.0 (D+)
Gramma Gerty: 44.0 (C)
Electric Android: 47.0 (B-)
Fred Bumpkin: 45.0 (A-)
Betty Rubble: 46.0 (C-)
Cecil Noshow: 43.0 (F)
Bif Buff: 50.0 (B+)
Andrew Airpump: 83.0 (A)
Jim Backus: 97.0 (A+)
Art Carnivore: 40.0 (D+)
Jim Dandy: 45.0 (C+)
Ima Elephant: 77.0 (B-)
Benny Franklin: 90.0 (B-)
Boy George: 4.0 (B)
Harvey Heffalump: 40.0 (C)