A spec compliant CSV parser in a (mostly) bare minimum of lines
Go to file
Emi Simpson 7be3c2311b
Collect code
Since this "project" was never really intended to be anything more than
a one-off project with a couple dozen lines tops, it was never developed
as a git repository.

Still, a week or two after the fact, I want to save it and maybe even
show it to people, so it's getting retroactively converted into a git
repo.
2024-03-17 12:35:59 -04:00
README.md Collect code 2024-03-17 12:35:59 -04:00
csv_parse.py Collect code 2024-03-17 12:35:59 -04:00
my_data.csv Collect code 2024-03-17 12:35:59 -04:00

README.md

A Spec-compliant CSV Parser in 17 lines of python

Just an exercise I did for fun. I wanted to see how practical it was to construct a spec-compliant CSV parser in python using parser combinators. Turns out, it's not that hard!

My end result was about 17 lines (two helper functions, two atomic parsers, seven combinators, and six parsers). Admittedly, there's a couple ugly lines. In particular, in the definition of the regex parser, I used lambda expressions as let expressions rather than employ a multi-line function definition, which looks a little sloppy. Still, I'm pretty happy with the result, especially given that conciseness was an explicit goal.

If you want to take a look, find the file in csv_parse.py or try cloning and running it. The sample data in this repository is stolen from this handy repo of sample CSV files.

It's worth noting that this was built purely as an exercise. While it is spec compliant and can theoretically actually be used, I would recommend using Python's built-in CSV parser, or just splitting on commas if you can get away with it. Please don't try to use this as a library.