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



我已经抓取了一个网站,结果是:

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

我需要在冒号前面的单词之前添加一个回车,例如。 \rGills:为了使信息更具可读性。

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

text_with_rc=re.sub(r'.s*',r'.rn',the_text)

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

str1=re.sub(r'(w*:)',r'rn1',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
>>> 

最新更新