Parsing a lisp file with Python

后端 未结 4 656
你的背包
你的背包 2021-01-02 15:02

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 th

4条回答
  •  Happy的楠姐
    2021-01-02 15:37

    Since the data is already in Lisp, use lisp itself:

    (let ((input '(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)))))
    
           (let ((row-headers (mapcar 'car (second input)))
              (row-data (mapcar (lambda (row) (mapcar 'second row)) (cdr input))))
    
         (format t "~{~A~^ ~}~%" row-headers)
         (format t "~{~{~A~^ ~}~^ ~%~}" row-data)))
    

提交回复
热议问题