Converting XML to CSV using ElementTree

As I have been working on cleaning the OpenStreetMap of Warsaw, I needed to convert chosen information from an XML file to CSV format.  While the operation in itself is rather straightforward, I find it a good opportunity to share a snippet of working code.

The OpenStreetMap of Warsaw can be downloaded from the website of the MapZen Project. If you take a look at the file (or its abbreviated version, since the original is quite large), you can see a mysterious field containing a ‘SIMC’ number.

As I got to know after a while of research, the number is referring to territorial classification of Polish cities. I found an XML file at the website of the Central Statistical Office of Poland which listed SIMC codes, but I needed the data in CSV format for easier comparison. The original data had the following format:

From all these information, I needed only the data contained in tags with names ‘NAZWA’, ‘SYM’ and ‘SYMPOD’. After a while of dabbling with the file, I came up with the following Python code:

At the beginning, I set the default encoding in order to display Polish characters properly. The code is very straightforward, and demands little commentary. It reads the entire XML document, and with every ‘col’ element checks the name of the field. If the name corresponds to one of the predefined ones, it saves it, and writes the saved names to the row once it reaches the end of the ‘row’ element.

The often helpful ElementTree has a detailed documentation, containing many examples of code. The Unicode problems, into which I ran the first time I fired the function, I solved thanks to a StackOverflow post. If you are curious about the OpenStreetMap project, take a look at their wiki, or download sample code from MapZen.

Rafal Kleczek

Leave a Reply