Wednesday, July 25, 2018

parse clj

if you've got a lob of Clojure data in a clj file, this gubbins will allow you to ingest it into python using the pyparsing library

            lkey = Suppress(Literal(":")) + Word(printables)
            lvalue = Forward()
            lvalue << (QuotedString(quoteChar='"', escChar='\\')
                       ^ Suppress('[') + Group(OneOrMore(QuotedString('"', escChar='\\'))) + Suppress(']')
                       ^ Suppress('{') + Dict(delimitedList(Group(lkey + lvalue), delim=',')) + Suppress('}'))
            row = Suppress("{") + Dict(delimitedList(Group(lkey + lvalue), delim=',')) + Suppress("}")

you call it with: cljdict = row.parseString(row_of_clj data)

No comments: