fpr=open("text.txt")
data=fpr.read()
n=0
for i in range(len(data)):
if data[i]=='/' and data[i+1]=='>':
n+=1
newdata=data[0:i]+str(n)+'>a'+str(n)+'fora'
k=i
break
for i in range(k+1,len(data)):
if data[i]=='/' and data[i+1]=='>':
n+=1
newdata=newdata+data[i-2:i]+str(n)+'>a'+str(n)+'fora'
newdata+='
fpr.close()
fpw=open("newtext.txt","w+")
fpw.write(newdata)
fpw.close()
测试数据:
输出结果:
测试文件
程序:
old=open("test.xml")
lines=old.readlines()
a=""
i=1
newlines=[]
for line in lines:
if a in line:
line=line.replace(a,"a"+str(i)+"for a")
i=i+1
newlines.append(line)
for line in newlines:
print line
new=open("newtest.xml","w")
new.writelines(newlines)
new.close()
old.close()
结果:
给分吧,哈哈。