簡體   English   中英

使用正則表達式在特定模式前插入回車

[英]Use Regex to insert a carriage return before a particular pattern

我在這里抓取了一個網站,結果如下:

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() 是最好的方法嗎?

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

是的,這需要 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
>>> 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM