Elisha512 Elisha512 - 2 months ago 8
Python Question

How to break a sentence in python depending on fullstop '.'?

I writing a script in python in which I have the following string:

a = "write This is mango. write This is orange."


I want to break this string into sentences and then add each sentence as an item of a list so it becomes:

list = ['write This is mango.', 'write This is orange.']


I have tried using TextBlob but it is not reading it correctly.(Reads the whole string as one sentence).

Is there a simple way of doing it?

Answer

One approach is re.split with positive lookbehind assertion:

>>> import re
>>> a = "write This is mango. write This is orange."
>>> re.split(r'(?<=\w\.)\s', a)
['write This is mango.', 'write This is orange.']

If you want to split on more than one separator, say . and ,, then use a character set in the assertion:

>>> a = "write This is mango. write This is orange. This is guava, and not pear."
>>> re.split(r'(?<=\w[,\.])\s', a)
['write This is mango.', 'write This is orange.', 'This is guava,', 'and not pear.']

On a side note, you should not use list as the name of a variable as this will shadow the builtin list.