The initial example of processing the input has two additional problems. Firstly, the header row is mixed in with the useful rows of data; this header row needs to be rejected by a filter of some kind. Secondly, the data is all strings, and some conversion is necessary. We'll solve each of these by extending the recipe.
There are two common techniques for discarding the unneeded header row:
- We can use an explicit iterator and discard the first item. The general idea is as follows:
with waypoints_path.open() as waypoints_file: raw_reader = csv.reader(waypoints_file) waypoints_iter = iter(waypoints_reader) next(waypoints_iter) # The header for row in waypoints_iter: print(row)
This snippet shows how to create an iterator ...