chapter nineteen

19 Workout Of the Day: Parsing CSV file, 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 pushups that day, I just get them done no matter how long it takes.[24]

[24] See "The Paradox of Choice" where increasing the number of choices given to people actually creates more distress and feelings of dissatisfaction is whatever choice is made. Imagine an ice cream shop with three flavors: chocolate, vanilla, and strawberry. If you choose chocolate, you’ll likely be happy with that choice. Now imagine the shop has 60 flavors of ice cream including 20 different fruit creams and sorbets and 12 different chocolate varieties from Rocky Road to Fudgetastic Caramel Tiramisu Ripple. Now when you choose a "chocolate" variety, you may leave with remorse about the 11 other kinds you could have chosen. Sometimes having no choice at all provides a sense of calm. Call it fatalism or whatnot.

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:

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  Potentials run-time errors

19.3.3  Using pandas.read_csv() to parse the file

19.3.4  Formatting the table

19.4  Review

19.5  Going Further