我试图弄清楚如何根据不同的句点,比如, . ? !,在python中将文本分割并保存到不同的句子中。但有些文本有小数点,re.split认为这是一个句号。我在想我怎么才能绕开它?任何帮助都将不胜感激!
示例文本:
A 0.75内径钢拉杆长4.8英尺,承载13.5kip.求出拉应力、总变形、单位应变和杆径变化。
发布于 2021-06-13 19:29:36
这将取决于您的输入,但是如果您可以假设您想要拆分的任何时间段后面都有一个空格,那么您可以简单地这样做:
>>> 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.']对于任何更复杂的内容,您可能需要使用这样的正则表达式:
import re
re.split(r'[\.!?]\s', s)https://stackoverflow.com/questions/67961911
复制相似问题