MS Excel and Python's csv moduleJun 29, 2012 Python Tweet
You may have noticed that I haven't posted in quite a while. Almost a year ago I started on some API work for an upcoming iPad app for a recording artist that has taken most of my focus and left me with not much time to participate in other projects. Unfortunately I'm not at liberty to talk about the specifics yet, as it is not due to release until the end of July.
Aside from coding the API, I've been doing a lot of database work on this project - schema changes and occasional massive record inserts/updates. In line with that, I'm working with a group on the business side who don't have much experience with how data is structured. I spend a lot of time cleaning up poorly formatted spreadsheets and turning the data rows into something that can easily be inserted into a database - against my will and better judgement, I've gotten very familiar with Excel.
Last week I got a spreadsheet that looked like this, with set list data from several years' worth of concerts:
And I needed to turn it into something like this (with a few other fields not shown here - basically, I needed to break the list of songs in a single cell into multiple rows of individual songs, with each song retaining the date and id metadata from the original row):
The songs in each cell were separated by a comma - I used this awesome Excel function to replace those with newlines (strictly optional - I just wanted to get a quick visual of how many lines of data I would be dealing with).
The list started out hundreds of lines long - broken out, it became tens of thousands of lines of song data.
To get to what I needed, my first step was to export from Excel to a .csv. The script I ran against the delimited file uses Python's csv module to read each line, then give each element from each cell its own row that contains the date and other metadata from the original line.
The script is stupid simple, I really just wanted a place to save it: