1

I'm trying to split a text file into sentences, using punctuation as a delimiter. The code I have so far works but the delimiter is being printed out on a line by itself. How can I keep the punctuation together with the sentence?

import re
string = ""
with open("text.txt") as file:
    for line in file:
        for l in re.split(r"(\. |\? |\! )",line):
            string += l + "\n"
print(string)

Example output:

This is the flag of the Prooshi — ous, the Cap and Soracer
. 
This is the bullet that byng the flag of the Prooshious
. 
This is the ffrinch that fire on the Bull that bang the flag of the Prooshious
.
mmkm
  • 69
  • 1
  • 7

1 Answers1

2

It's actually were simple, you adding \n(newline character) on every iteration, so, for example you splitting Kek. it will add to string variable Kek\n and then .\n. You need to do something like this:

with open("text.txt") as file:
for line in file:
    for l in re.split(r"(\. |\? |\! )",line):
        string += l
    string += '\n'
Yarick
  • 326
  • 3
  • 14