python - 如何根据句号 '.' 在 python 中中断一个句子?

标签 python list

我在 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/

相关文章:

python - BeautifulSoup - 抓取多个页面

python - Pandas 如何获取多索引数据帧中索引级别具有多个值的行列表

python - 填充 numpy 数组的元素

python - 根据python中元组第一个元素的长度对元组列表进行排序

python - 怎样才能取出元素的乘积呢?

Python,如何在列表末尾不需要额外的空间?

python - 使用一天中的特定时间或小时过滤 Pandas 数据框

Python 非 numpy 矩阵问题

python - Python 中 k、v 元组列表中的唯一组合

python - htaccess 导致 python cgi 出现问题