简体   繁体   中英

Use Regex to insert a carriage return before a particular pattern

I have scraped a website here is the result:

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

I need to add a carriage before the words which precede the colon eg. \\r\\nGills: in order to make the information more readable.

Is re.sub() the best way to do this?

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

Yes re.sub is required for this. Try the below pattern

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
>>> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM