[Tutor] delete strings from specificed words

Cameron Simpson cs at cskk.id.au
Thu Jan 11 03:20:42 EST 2018


On 11Jan2018 12:16, YU Bo <tsu.yubo at gmail.com> wrote:
>Hi,

Hi,

>On Thu, Jan 11, 2018 at 11:40:35AM +1100, Cameron Simpson wrote:
>>Do you have the text as above - a single string - or coming from a
>>file? I'll presume a single string.
>
>En.., the text is  multi-string from str(something) within python.

That's a single string in my thinking.

>>I would treat the text as lines, particularly since the diff markers
>>etc are all line oriented.
>>
>>So you might write something like this:
>>
>>interesting = []
>>for line in the_text.splitlines():
>>  if line.startswith('diff --git '):
>>    break
>>  interesting.append(line)
>>
>>Now the "interesting" list has the lines you want.
>
>Yes, i test your method  and it will come to my intend, but maybe cast
>a few of other steps.
>
>I think that the method will offer a similar ways to solve others issues.
>I personally like peter's method: text.partition("diff  --git")[0].strip()

Yes, Peter's partition method is very short and direct. Personally I would 
include a leading newline in the string, thus:

  text.partition("\ndiff --git ")

to make it more precise. Consider a patch which discusses your script; its 
comment area might well mention the string "diff --git", as this email does.

What you end up with may depend on what further things you do with the text.

Cheers,
Cameron Simpson <cs at cskk.id.au>


More information about the Tutor mailing list