首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >分割和保存输入文本的部分

分割和保存输入文本的部分
EN

Stack Overflow用户
提问于 2021-06-13 19:23:15
回答 1查看 40关注 0票数 1

我试图弄清楚如何根据不同的句点,比如, . ? !,在python中将文本分割并保存到不同的句子中。但有些文本有小数点,re.split认为这是一个句号。我在想我怎么才能绕开它?任何帮助都将不胜感激!

示例文本:

A 0.75内径钢拉杆长4.8英尺,承载13.5kip.求出拉应力、总变形、单位应变和杆径变化。

EN

回答 1

Stack Overflow用户

发布于 2021-06-13 19:29:36

这将取决于您的输入,但是如果您可以假设您想要拆分的任何时间段后面都有一个空格,那么您可以简单地这样做:

代码语言:javascript
复制
>>> s = 'A 0.75-in-diameter steel tension rod is 4.8 ft long and carries a load of 13.5 kip. Find the tensile stress, the total deformation, the unit strains, and the change in the rod diameter.'
>>> s.split('. ')
['A 0.75-in-diameter steel tension rod is 4.8 ft long and carries a load of 13.5 kip', 'Find the tensile stress, the total deformation, the unit strains, and the change in the rod diameter.']

对于任何更复杂的内容,您可能需要使用这样的正则表达式:

代码语言:javascript
复制
import re
re.split(r'[\.!?]\s', s)
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67961911

复制
相关文章

相似问题

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