Python的XML节点替换或字符串替换问题

2024-12-22 17:05:56
推荐回答(2个)
回答1:

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()

测试数据:text
输出结果:texta1foraa2foraa3fora