python 循环替换问题求助?

循环替换图片地址遇到问题:
 
代码如下:
 
#! /usr/bin/env python
# coding:utf-8
import MySQLdb

raw_img = ['http://p3.pstatp.com/large/242d000048b8442a96d0','http://p3.pstatp.com/large/242d000048b8442a96d1']
new_img = ['0.png','1.png']
article_main = '<img alt="朋友圈现“神药”可控孩子性别,专家:呵呵!" img_height="340" img_width="600" inline="0" src="http://p3.pstatp.com/large/242d000048b8442a96d1"/><img alt="朋友圈现“神药”可控孩子性别,专家:呵呵!" img_height="340" img_width="600" inline="0" src="http://p3.pstatp.com/large/242d000048b8442a96d0"/>;'
k = 0

for i in raw_img:
new_article_main = article_main.replace(i,new_img[k]).replace(i,new_img[k])
k += 1

print new_article_main
exit()

结果:
 
<img alt="朋友圈现“神药”可控孩子性别,专家:呵呵!" img_height="340" img_width="600" inline="0" src="1.png"/><img alt="朋友圈现“神药”可控孩子性别,专家:呵呵!" img_height="340" img_width="600" inline="0" src="http://p3.pstatp.com/large/242d000048b8442a96d0"/>

怎么样才能得到这样的结果:
 
<img alt="朋友圈现“神药”可控孩子性别,专家:呵呵!" img_height="340" img_width="600" inline="0" src="1.png"/><img alt="朋友圈现“神药”可控孩子性别,专家:呵呵!" img_height="340" img_width="600" inline="0" src="0.png"/>
已邀请:

哚颜三卫

赞同来自:

已经自行解决,机智如我:
 
#! /usr/bin/env python
# coding:utf-8
import MySQLdb
import re

raw_img = ['http://p3.pstatp.com/large/242d000048b8442a96d0','http://p3.pstatp.com/large/242d000048b8442a96d1']
new_img = ['0.png','1.png']
article_main = '<img alt="朋友圈现“神药”可控孩子性别,专家:呵呵!" img_height="340" img_width="600" inline="0" src="http://p3.pstatp.com/large/242d000048b8442a96d1"/><img alt="朋友圈现“神药”可控孩子性别,专家:呵呵!" img_height="340" img_width="600" inline="0" src="http://p3.pstatp.com/large/242d000048b8442a96d0"/>;'
k = 0
replace_command = ''

for i in raw_img:
replace_command += '.replace'+'("'+i+'","'+new_img[k]+'")'
k += 1

article_main = eval(("article_main"+replace_command))

print article_main
exit()

要回复问题请先登录注册