Convert tabbed text to html unordered list?

前端 未结 4 651
难免孤独
难免孤独 2021-01-03 03:11

I\'m a beginner programmer so this question might sound trivial: I have some text files containg tab-delimited text like:

A
    B
    C
        D
        E
<         


        
4条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-03 04:16

    Try this (works on your test case):

    import itertools
    def listify(filepath):
        depth = 0
        print "
      "*(depth+1) for line in open(filepath): line = line.rstrip() newDepth = sum(1 for i in itertools.takewhile(lambda c: c=='\t', line)) if newDepth > depth: print "
        "*(newDepth-depth) elif depth > newDepth: print "
      "*(depth-newDepth) print "
    • %s
    • " %(line.strip()) depth = newDepth print "
    "*(depth+1)

    Hope this helps

提交回复
热议问题