Yarin
Yarin

Reputation: 183469

Parse multi-line string up until first line with certain character

I want to parse out all lines from a multi-line string up until the first line which contains an certain character- in this case an opening bracket.

s = """Here are the lines 
of text that i want.
The first line with <tags> and everything
after should be stripped."""

s1 = s[:s.find('<')]
s2 = s1[:s.rfind('\n')]

print s2

Result:

Here are the lines
of text that i want.
The first line with

What I'm looking for:

Here are the lines
of text that i want.

Upvotes: 0

Views: 2293

Answers (1)

mgilson
mgilson

Reputation: 309831

change

s2 = s1[:s.rfind('\n')]  #This picks up the newline after "everything"

to

s2 = s1[:s1.rfind('\n')]  

and it will work. There might be a better way to do this though...

Upvotes: 2

Related Questions