I am trying to replace certain parts of the string below.
'''<td align="center"> 5 </td> <td> align="center"> 0.0001 </td>'''
I need to remove the <td>
tag if there is a '0.'(decmial occurrence). i.e. the output should be
'''<td align="center"> 5 </td>'''
I have tried this
data = ' '.join(data.split())<br>
l = data.replace('<td align="center"> 0.r"\d" </td>', "")
but didn't succeed. Could anyone please help me with doing this.
Thanks in advance
While both of the regular expression examples work, I would advice against using regexp.
Especially if the data is a full html document, you should go for html-aware parser, such as lxml.html
e.g.:
from lxml import html
t = html.fromstring(text)
tds = t.xpath("table/tbody/tr[2]/td")
for td in tds:
if tds.text.startswith("0."):
td.getparent().remove(td)
text = html.tostring(t)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With