19 Workout of the Day: Parsing CSV files, creating text table output

 

Several years ago, I joined a workout group. We meet several times a week in our coach’s unpaved driveway. We pick up and drop heavy things and run around trying to keep Death at bay for another day. I’m no paragon of strength and fitness, but it’s been a nice way to exercise and visit with friends. One of my favorite parts of going is that our coach will write a “Workout of the Day” or “WOD” on the board. Whatever it says is what I do. It doesn’t matter if I actually want to do 200 push-ups that day, I just get them done no matter how long it takes.1


In that spirit, we’ll write a program called wod.py to help us create a random daily workout that we have to do, no questions asked:

$ ./wod.py
Exercise              Reps
------------------  ------
Pushups                 40
Plank                   38
Situps                  99
Hand-stand pushups       5
Note

Each time you run the program, you are required to perform all the exercises immediately. Heck, even just reading them means you have to do them. Like NOW. Sorry, I don’t make the rules. Better get going on those sit-ups!

19.1 Writing wod.py

19.1.1 Reading delimited text files

19.1.2 Manually reading a CSV file

19.1.3 Parsing with the csv module

19.1.4 Creating a function to read a CSV file

19.1.5 Selecting the exercises

19.1.6 Formatting the output

19.1.7 Handling bad data

19.1.8 Time to write

19.2 Solution

19.3 Discussion

19.3.1 Reading a CSV file

19.3.2 Potential runtime errors

19.3.3 Using pandas.read_csv() to parse the file

19.3.4 Formatting the table