Parse a CSV file in Python the hard way

Did you ever want to parse a CSV file in Python without using the CSV module? Wait no longer, as below is the description of a simple CSV parser, which could be handy if the simplicity of the actions you want to perform on it does not warrant importing the CSV module.


The code shown below can perform simple parsing operations on CSV files, provided there is no inconsistencies in it. If the code returns an error, the possible cause is the presence of inconsistent formatting of the file, in which case importing the CSV module would be necessary:


The output of the function will be a list of dictionaries, in which keys are the column headers, forming the first line of the file, and the values are the values of subsequent lines.


I would like to devote a few words of commentary to the code presented above. The code divides the file by commas, which can be changed by substituting the comma in the two ‘split’ methods to the character used in the file to divide values.

Using the ‘zip’ method to join keys and values is one of several ways to accomplish it. You can read about it, as well as about other methods, in the interesting article by Nick Dunn.


If you are participating in Udacity courses, you might recognize the idea for the parser from the wonderful data wrangling course, which I highly recommend. My code is different from the one shown in the course, and it could be interesting to compare it with the one shown by the course guide, Shannon Bradshaw.

Rafal Kleczek

Leave a Reply