Getting ready

Converting a file of data from one format to another means that the program will need to have two open contexts: one for reading and one for writing. Python makes this easy. The use of with statement contexts assures that the files are properly closed and all of the related OS resources are completely released.

We'll look at a common problem of summarizing many web log files. The source is in a format that we've seen in the Writing generator functions with the yield statement recipe in online Chapter 12Functional And Reactive Programming Features (link provided in Preface), and also Reading complex formats using regular expressions recipe in this chapter. The rows look like the following:

 [2016-05-08 11:08:18,651] INFO in ...

Get Modern Python Cookbook now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.