Authors Note: This code has been updated and improved upon. I won’t remove this post, but I highly suggest you use the code from the improved code in the post found here.
Last night I had a project where the client needed 30 .csv files compiled into a single sheet. I figured that I could concatenate them together using Python real quick. Python makes tasks like this fairly trivial, but I like to experiment a bit so rather than just write a loop appending each file I wrote a class to do it with some methods. It’s reusable for anyone who has a similar task. I had wanted to add a utility which would compare the headers of 1 file with the headers of the previous file but I was on the clock so I’ll save that bit for another day.
Python has a
csv module but I don’t use it in this class yet. The Python csv module is really just a utility in itself to make working with CSV text files easier. Since there is no difference between a regular text file and a csv file and I know the format of the files being fed to the class I simply used Pythons
open method to grab a handle to my files. I did import the
csv python module though to remind me to use it when I extend this in the future.