0

I want to replace all occurrences of double quotes with single quotes. But only in the img tag! I have a html text

<p>First p</p><img class="image" src="one.jpg" />
<p>Second p</p><img class="image" src="two.jpg" />

How can I replace this "in place". I tried something like this:

re.sub('"', "'", re.findall(r'<img.*/>', html))

The expected result is this:

<p>First p</p><img class='image' src='one.jpg' />
<p>Second p</p><img class='image' src='two.jpg' />
Cœur
  • 34,719
  • 24
  • 185
  • 251
saromba
  • 432
  • 1
  • 6
  • 20

1 Answers1

0

re.findall() returns a list, while re.sub() expects a string as the input

r=re.findall(r'<img.*>', html)
b=[re.sub('"', "'",a) for a in r]
for i in range(len(b)):
    html=str.replace(html,r[i],b[i])
print html

Output

<p>First p</p><img class='image' src='one.jpg' />
<p>Second p</p><img class='image' src='two.jpg' />
Hari Krishnan
  • 1,977
  • 2
  • 15
  • 28