Skip to main content
 首页 » 编程设计

python之用大写字母拆分句子

2025年05月04日14zhenyulu

我该如何拆分?

'Symptoms may include:Absent or small knucklesCleft palateDecreased skin creases at finger jointsDeformed earsDroopy eyelidsInability to fully extend the joints from birth (contracture deformity)Narrow shouldersPale skinTriple-jointed thumbs' 

期望的输出应该采用这种形式

Symptoms may include: 
Absent or small knuckles 
Cleft palate 
Decreased skin creases at finger joints 
Deformed ears 
Droopy eyelids 
Inability to fully extend the joints from birth (contracture deformity) 
Narrow shoulders 
Pale skin 
Triple-jointed thumbs 

就像拆分大写字母一样。

请您参考如下方法:

使用 re.findall(感谢@Brendan Abel 和@JFF 改进了模式):

fragments = re.findall('[A-Z][^A-Z]*', text) 

print(fragments) 
['Symptoms may include:', 
 'Absent or small knuckles', 
 'Cleft palate', 
 'Decreased skin creases at finger joints', 
 'Deformed ears', 
 'Droopy eyelids', 
 'Inability to fully extend the joints from birth (contracture deformity)', 
 'Narrow shoulders', 
 'Pale skin', 
 'Triple-jointed thumbs'] 

详情

[A-Z]      # match must begin with a uppercase char 
[^A-Z]*    # further characters in match must not contain an uppercase char 

注意:* 允许您捕获包含单个大写字符的句子。如果这不是所需的功能,请替换为 +。

此外,如果您希望输出为多行字符串:

print('\n'.join(fragments))