0

複数行の文字列から、特定の文字 (この場合は開き括弧) を含む最初の行までのすべての行を解析したいと考えています。

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

結果:

ここに
私が欲しいテキストの行があります。
との最初の行

私が探しているもの:

ここに
私が欲しいテキストの行があります。

4

1 に答える 1

2

変化する

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

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

そしてそれは動作します。しかし、これを行うためのより良い方法があるかもしれません...

于 2012-06-04T13:09:52.970 に答える