0

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.

Yarin
  • 173,523
  • 149
  • 402
  • 512

1 Answers1

2

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...

mgilson
  • 300,191
  • 65
  • 633
  • 696
  • oh dumb- that's what i meant to do, thanks (you need rfind for the newline part though- otherwise you'd only get the first line) – Yarin Jun 04 '12 at 13:16
  • @Yarin -- Yes, you need `rfind` for the newline part -- not the `'<'` part. (that's what I meant by the *first* `rfind` -- sorry if that wasn't clear -- Edited to make it slightly more explicit). – mgilson Jun 04 '12 at 13:17
  • @Yarin -- Oops, apparently you had it as `find` all along. I don't know why I saw `rfind` in both cases. Sorry. (edited again to remove my useless (and wrong) comment). – mgilson Jun 04 '12 at 13:19