在python中使用异常拆分

时间:2018-05-28 06:40:39

标签: python

Mt. Everest is the highest peak in the world. It's height is 8848 m. 

我如何将其分成两句话?除了' Mt'?之外不要把它分开。规则是每当Mt跟随一个点时,它需要跳过它。

这样的事情:

Mt. Everest is the highest peak in the world. 
It's height is 8848 m.
到目前为止

代码

data = "Mt. Everest is the highest peak in the world. It's height is 8848 m."
sentences=data.split(".")
print (sentences)

1 个答案:

答案 0 :(得分:2)

使用负向外观以避免在Mt之后出现点时分裂。

 re.split('(?<!Mt)\. ', s)

<强>代码

>>> s = "Mt. Everest is the highest peak in the world. It's height is 8848 m."
>>> re.split('(?<!Mt)\. ', s)
['Mt. Everest is the highest peak in the world', "It's height is 8848 m"]