如何在Python中的字符串中有效地加倍HTML <img/>的维度?

时间:2011-05-04 03:13:56

标签: python regex html-manipulation

  

可能重复:
  how to make the width and height x2 using python Regular

这是我的代码:

import re

s = '<img src = "/" width="10" height="111"  />'


def a(x):
    b = x.group(0)
    b = b.replace(x.group(1),str(int(x.group(1))*2))
    b = b.replace(x.group(2),str(int(x.group(2))*2))
    return b

ss = re.sub(r'''<img.*?width=\"(\d+)\".*?height=\"(\d+)\"[^>]*>''',a, s)

print ss

它运作正常:

<img src = "/" width="20" height="222"  />

但是如果我把height属性放在宽度之前,就像这样:

<img src = "/" height="111" width="10" />

它什么都不做。

所以我必须这样做,尝试两种可能性:

import re

s = '<img src = "/" height="111" width="10" />'


def a(x):
    c = x.group(0)
    c = c.replace(x.group(1),str(int(x.group(1))*2))
    return c

def b(x):
    c = x.group(0)
    c = c.replace(x.group(1),str(int(x.group(1))*2))
    return c

ss = re.sub(r'''<img.*?width=\"(\d+)\"[^>]*>''',a, s)
print ss
ss = re.sub(r'''<img.*?height=\"(\d+)\"[^>]*>''',b, ss)
print ss
但是我的老板告诉我它会花很多时间。

那我该怎么办?

感谢。

0 个答案:

没有答案