使用正则表达式在特定模式之前插入回车符

问题描述 投票:-1回答:2

我在这里抓了一个网站就是结果:

Description
Cap: 4-9 cm Pale brown - often lighter towards the edge. 
Viscid when wet.Gills: Pale clay-brown. Free. Gill edge exude droplets 
when moist which dry to form dark spots.Stem: Off-white. Mealy towards 
the apex.Spores: Clay-brownFlesh: Firm, white. Smell of 
radishesHabitat: In groups or rings on the ground in mixed 
woodlandFrequency: Very Common

我需要在冒号之前的单词之前添加一个滑架。 \ r \ nGills:为了使信息更具可读性。

re.sub()是最好的方法吗?

python regex
2个回答
0
投票
text_with_rc=re.sub(r'\.\s*',r'.\r\n',the_text)

0
投票

是的,需要re.sub。尝试以下模式

str1=re.sub(r'(\w*:)',r'\r\n\1',string)
>>> print str1
Description


Cap: 4-9 cm Pale brown - often lighter towards the edge. 
Viscid when wet.

Gills: Pale clay-brown. Free. Gill edge exude droplets 
when moist which dry to form dark spots.

Stem: Off-white. Mealy towards 
the apex.

Spores: Clay-

brownFlesh: Firm, white. Smell of 


radishesHabitat: In groups or rings on the ground in mixed 


woodlandFrequency: Very Common
>>> 
© www.soinside.com 2019 - 2024. All rights reserved.