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
  • 37,241
  • 25
  • 195
  • 267
saromba
  • 472
  • 2
  • 7
  • 23

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
  • 2,049
  • 2
  • 18
  • 29