检查下一行是否比当前行有更多前导空格

时间:2019-06-09 00:06:45

标签: python python-3.x

我有一个这样的文本文件,我需要通读每一行。

XXXX
....
YYYY
    ZZZZ
    ZZZZ
    ....
YYYY
    ZZZZ
    ZZZZ
    ....

....指任意数量的上述对象。

文件被读入名为textList的列表中

lines = enumerate(textList)
for i, line in lines:
    #read in the XXXXs
    if line == "YYYY"
        # from this location, get the next line in lines
        # i.e. (i, lines = lines.__next__())
        # until the next line has the same or lower amount
        # of whitespace than the amount of whitespace before
        # the line that has "YYYY"

我已经尝试了几种不同的方法,但是我不断遇到无限循环。我知道这是可能的,但我只是无法弄清楚

当前脚本:(提供索引错误)

def getIndentLevel(line):
    tabCount = len(line) - len(line.lstrip(' '))
    #print(tabCount)
    return tabCount

textList = ["XXXX", 
            "XXXX", 
            "XXXX",
            "YYYY",
            "    ZZZZ",
            "    ZZZZ",
            "YYYY",
            "    ZZZZ",
            "    ZZZZ"]
lines = enumerate(textList)

for i, line in lines:
    if line.lstrip(' ') == "YYYY":
        print("YYYY found")
        cur = getIndentLevel(textList[i])
        while True:
            nxt = getIndentLevel(textList[i+1])
            if nxt <= cur:
                break
            i, line = lines.__next__()
            print(i, line)
            #nxt = getIndentLevel(textList[i+1])

2 个答案:

答案 0 :(得分:1)

我认为您无需枚举。

for  i in range(len(textList)) :   
  numWhiteSpace = len(textList[i]) - len(textList[i].lstrip())
  if i == 0:
    continue
  else:
    if numWhiteSpace > (len(textList[i-1]) - len(textList[i-1].lstrip())):
      print(textList[i])

答案 1 :(得分:0)

此脚本解决了问题:

def getIndentLevel(line):
    tabCount = len(line) - len(line.lstrip(' '))
    return tabCount

textList = ["XXXX", 
            "XXXX", 
            "XXXX",
            "YYYY",
            "    ZZZZ",
            "    ZZZZ",
            "YYYY",
            "    ZZZZ",
            "    ZZZZ",
            "",
            "",
            "",
            "XXXX",
            "XXXX",]
lines = enumerate(textList)

for i, line in lines:
    if line.lstrip(' ') == "YYYY":
        print("YYYY found")
        cur = getIndentLevel(textList[i])
        nxt = getIndentLevel(textList[i+1])
        while True:
            if nxt <= cur:
                break
            i, line = lines.__next__()
            print(i, line)
            try:
                nxt = getIndentLevel(textList[i+1])
            except:
                break