Skip to content Skip to sidebar Skip to footer

Python Read Specific Lines Of Text

I am having a problem reading specific lines. It's similar to the question answered here: python - Read file from and to specific lines of text The difference, I don't have a fixed

Solution 1:

How about this:

defgetBlocks(filepath):
    withopen(filepath) as f:
        blocks = []
        go = Falsefor line in f:
            if line.strip() == startDelimiter:
                block = ''
                go = Trueif go:
                block += line
            if line.strip() == endDelimiter:
                blocks.append(block)
                block = ''
                go = Falseif block:
            blocks.append(block)
    return blocks

Solution 2:

The beauty is that if you hit EOF, the file will stop iterating.

ended=Falsefor line in f:ended=line==MY_END_MARKER

Solution 3:

Post a Comment for "Python Read Specific Lines Of Text"