Motoma.io

A Simple Extension to the Python CSV Object

One of the side effects of working with database driven software is that you eventually find yourself needing to pull in large amounts of information from old and terrible systems. When talking to your counterparts on the other side of the line (the inter-company line, that is), you will invariably be told that you will only receive your data in one of a few straight forward formats. What follows is a small extension to Python's CSV object which streamlines the process of coding these data transformations.

Simply put, Python's CSV object is more than enough to handle any conversion; however, in order to make my life even simpler, I made the following wrapper:

Here, the process accepts a function object, and an argument (tuple most likely). The CSVFileHandler object will execute the function on each line of the file, making the main loop of your processing code a meek 3 lines long. Take, for example, the following code:

The code listing above quickly parses through that old CSV file and leaves with with a shiney new SQL data file to load into your database.

comments powered by Disqus