首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python:句子分割会产生一个空格

Python:句子分割会产生一个空格
EN

Stack Overflow用户
提问于 2013-01-14 04:44:07
回答 4查看 156关注 0票数 0

所以我有一些句子,比如:

代码语言:javascript
复制
The window is over there. The lamp is on. The fire is burning.

当我使用split('.')拆分它时然后用换行符连接它,它就失去了".“

然后我尝试了像(?<=\.)\s这样的正则表达式,但它在第二个和第三个字母的第一个字母之前产生了一个空格:

代码语言:javascript
复制
The window is over there.
 The lamp is on.
 The fire is burning.

我不想要额外的空间。我想要:

代码语言:javascript
复制
The window is over there.
The lamp is on.
The fire is burning.

谢谢

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2013-01-14 04:50:01

代码语言:javascript
复制
".\n".join(i.strip() for i in a.split("."))
票数 3
EN

Stack Overflow用户

发布于 2013-01-14 04:49:04

代码语言:javascript
复制
>>> test = "The window is over there. The lamp is on. The fire is burning."
>>> print test.replace(". ",".\n")
The window is over there.
The lamp is on.
The fire is burning.
票数 3
EN

Stack Overflow用户

发布于 2013-01-14 04:50:03

显然没有处理特殊情况(即句点后没有空格),为什么不直接处理:

代码语言:javascript
复制
>>> s = 'The window is over there. The lamp is on. The fire is burning.'
>>> print s.replace('. ', '.\n')
The window is over there.
The lamp is on.
The fire is burning.
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14307949

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档