0

I have a string that has allot of blank lines, I need to return the chunk of that string that is found before the first blank line in it. For Example:

aaaaa
bbbb
1223

212
fff

The returned string should be:

aaaaa
bbbb
1223

Note: I am using Python 2.7

Ma0
  • 15,057
  • 4
  • 35
  • 65
Elias Shourosh
  • 89
  • 3
  • 14
  • 1
    [split](https://docs.python.org/2/library/stdtypes.html#str.split) on `\n\n` or whatever the line separator character is **doubled** and get the 0-index element. – Ma0 Jun 06 '18 at 14:55

2 Answers2

1
def find(string):
    return string[:string.find('\n\n')]
Kevin
  • 61
  • 5
  • check out [this post](https://stackoverflow.com/questions/1883980/find-the-nth-occurrence-of-substring-in-a-string) You could also just try something like: `return string[1:string.find('\n\n')]` – Kevin Jun 06 '18 at 15:07
1

This is one approach. Using a simple iteration.

Demo:

res = []
with open(filename, "r") as infile:
    for line in infile:
        if not line.strip():
            break
        else:
            res.append(line)
print( "".join(res) )
Rakesh
  • 81,458
  • 17
  • 76
  • 113