aatifh
aatifh

Reputation: 2337

How to embed p tag inside some text using Beautifulsoup?

I wanted to embed <p> tag where ever there is a \r\n\r\n.

u"Finally Sri Lanka showed up, prevented their first 5-0 series whitewash, and stopped India at nine ODI wins in a row. \r\n\r\nFor 62 balls Yuvraj Singh played a dream knock, keeping India in the game despite wickets falling around him. \r\n\r\nPerhaps the toss played a big part. This was only the second time Mahela Jayawardene beat Mahendra Singh Dhoni with the coin in the last 11 occasions. \r\n\r\nIt was Jayasuriya who provided Sri Lanka with the springboard. \r\n\r\nThe pyrotechnics may have stopped upon Jayasuriya's dismissal, but the runs kept coming at a fair pace."

I tried solving this using BeautifulSoup but couldn't find the way out of it. Can anyone through some light on this. Thanks in advance.

Upvotes: 2

Views: 307

Answers (1)

nosklo
nosklo

Reputation: 222862

''.join('<p>%s</p>' % line for line in text.split('\r\n\r\n'))
# Results:
u"<p>Finally Sri Lanka showed up, prevented their first 5-0
series whitewash, and stopped India at nine ODI wins in a row. </p>
<p>For 62 balls Yuvraj Singh played a dream knock, keeping India in the 
game despite wickets falling around him. </p><p>Perhaps the toss played
a big part. This was only the second time Mahela Jayawardene beat Mahendra
Singh Dhoni with the coin in the last 11 occasions. </p>
<p>It was Jayasuriya who provided Sri Lanka with the springboard. </p>
<p>The pyrotechnics may have stopped upon Jayasuriya's dismissal, but 
the runs kept coming at a fair pace.</p>"

Upvotes: 5

Related Questions