Home > Back-end >  Formatting a list of sentences extracted from text
Formatting a list of sentences extracted from text

Time:09-20

I am trying to format a list neatly that I have extracted using regex. I would like to have each sentence in its own line and remove the \n characters:

words = ['billion']
sentences = [sentence for sentence in text_1 if any(
    w.lower() in sentence.lower() for w in words)]

print(sentences)

Image of current output:

enter image description here

CodePudding user response:

From OP's image, text_1 is a list of strings. To remove the newline \n characters from a string, you can use the string's replace method. To print each newline-removed sentence on its own line, you can use a simple for loop. Keeping the rest of the code intact, replace print(sentences) with:

for s in sentences:
    print(s.replace('\n', ''))
  • Related