I have the following lisp file, which is from the UCI machine learning database. I would like to convert it into a flat text file using python. A typical line looks like this:
(1 ((st 8) (pitch 67) (dur 4) (keysig 1) (timesig 12) (fermata 0))((st 12) (pitch 67) (dur 8) (keysig 1) (timesig 12) (fermata 0)))
I would like to parse this into a text file like:
time pitch duration keysig timesig fermata
8 67 4 1 12 0
12 67 8 1 12 0
Is there a python module to intelligently parse this? This is my first time seeing lisp.
Since the data is already in Lisp, use lisp itself:
If you know that the data is correct and the format uniform (seems so at a first sight), and if you need just this data and don't need to solve the general problem... then why not just replacing every non-numeric with a space and then going with split?
Separate it into pairs with a regular expression:
Then make it into a dictionary:
The result:
Printing:
Proper formatting is left as an exercise for the reader...
As shown in this answer, pyparsing appears to be the right tool for that:
For the completeness' sake, this is how to format the results (using texttable):
To convert that data back to the lisp notation: