我在 python 中编写了一个脚本,其中包含以下字符串:
a = "write This is mango. write This is orange."
我想把这个字符串分成句子,然后将每个句子添加为列表的一个项目,这样它就变成了:
list = ['write This is mango.', 'write This is orange.']
我试过使用 TextBlob,但它没有正确读取它。(将整个字符串作为一个句子读取)。
有什么简单的方法吗?
最佳答案
一种方法是 re.split
使用正面回顾断言:
>>> 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.']
如果您想在多个分隔符上拆分,例如.
和,
,然后在断言中使用一个字符集:
>>> 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.']
附带说明一下,您不应该使用 list
作为变量的名称,因为这将遮盖内置的 list
。
关于python - 如何根据句号 '.' 在 python 中中断一个句子?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39441157/