MarkF6
MarkF6

Reputation: 503

HTML and other formatting

is there a way (using python and lxml) to get an output of HTML code like this:

<table class=main>
<tr class=row>
</tr>
</table>

instead of one like this one:

<table class=main><tr class=row></tr>
</table>

Only tags named "span" in div-tags can be appended. So things like:

<div class=paragraph><span class=font48>hello</span></div>

are allowed. Thanks a lot for any help.

Upvotes: 0

Views: 50

Answers (3)

msturdy
msturdy

Reputation: 10794

Have you considered the prettify() method from the module BeautifulSoup ?

#!/usr/bin/env python
from BeautifulSoup import BeautifulSoup as bs

html = '<table class=main><tr class=row></tr>\
</table>'

print bs(html).prettify()

outputs:

<table class="main">
 <tr class="row">
 </tr>
</table>

Note - it will add some indentation to the output, as you can see.

Upvotes: 0

4d4c
4d4c

Reputation: 8169

Another option would be using BeautifulSoup:

from bs4 import BeautifulSoup    
html = "<table class=main><tr class=row></tr></table>"    
soup = BeautifulSoup(html)    
print soup.prettify()

Output:

<table class="main">
 <tr class="row">
 </tr>
</table>

Upvotes: 2

Dean Elbaz
Dean Elbaz

Reputation: 2450

you could insert a line break before every "<" with a regex

Upvotes: 2

Related Questions