Parsing a lisp file with Python

后端 未结 4 639
你的背包
你的背包 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条回答
  •  隐瞒了意图╮
    2021-01-02 15:55

    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?

    import re
    data = open("chorales.lisp").read().split("\n")
    data = [re.sub("[^-0-9]+", " ", x) for x in data]
    for L in data:
        L = map(int, L.split())
        i = 1  # first element is chorale number
        while i < len(L):
            st, pitch, dur, keysig, timesig, fermata = L[i:i+6]
            i += 6
            ... your processing goes here ...
    

提交回复
热议问题