Remove number of lines from string in python

Question:

for example below is the string

news="Waukesha trial: US man sentenced to life for car-ramming attack - BBC NewsBBC HomepageSkip to contentAccessibility HelpYour accountHomeNewsSportReelWorklifeTravelFutureMore menuMore menuSearch BBCHomeNewsSportReelWorklifeTravelFutureCultureMusicTVWeatherSoundsClose menuBBC NewsMenuHomeWar in ArtsHealthWorld News TVIn PicturesReality CheckNewsbeatLong ReadsUS  CanadaUS Elections 2022ResultsWaukesha trial: US man sentenced to life for car-ramming attackPublished11 hours agoSharecloseShare pageCopy linkAbout sharingThis video can not be playedTo play this video you need to enable JavaScript in your browser Media caption, Watch: Emotional testimony from victims familiesA Wisconsin judge has sentenced a man who killed six people and injured 62 by driving through a Christmas parade last year to life in prison A jury convicted Darrell Brooks last month after prosecutors argued he had shown "utter disregard for human life" The court also heard emotional testimony from dozens of survivors and families of victims during sentencing Brooks represented himself in the four-week trial, often interrupting court proceedings In her sentencing on Wednesday, Judge Jennifer Dorow said Brooks had chosen  path of evil"`

I want to remove 1st 3 sentences from above string. If the words are ending with . or : or – It means one sentence.

How to do that?

Asked By: Martin Luther

||

Answers:

First, you need to escape double quotes, your string is not valid like this.
Second, are you sure the words ending with "-" mean the end of a sentence? In your example you would split "car-ramming" and "four-week". Anyway, you can split the string into sentences like this:

sentences = news.replace('-','.').split('.')

You would get a list of your sentences like [sentence1, sentence2…], which you could slice to remove the first 3:

new_sentences = sentences[3:]

You can then get a string from the new list like this:

". ".join(new_sentences)

The only problem is that you would replace all the "-" for ".", but as I said, I think the "-" does not actually indicate the end of a sentence in your example.

Another way would be to find your separators 3 times, and remove all the characters to that position:

for i in range(3):
    point = news.find(".")
    minus = news.find("-")
    index =  min(point,minus)
    if index == -1:
        index = max(point,minus)
    news = news[index+1:]
Answered By: jfchuman
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.