简体   繁体   中英

How to find and replace different strings in a file at a time?

As per my know this may work for one condition but it is clears my file and replacing the find string in file.

 #!/usr/bin/python
    open('vpc.xml','r+') as p: #open a file in read and write mode
    p=rp.readlines()
    print p #printing what content read by p
    for line in p: #for loop up to end of line in file
          if "<mac address=" in  line: #if condition encounter by for loop line then it replaces the string specified in if condition.
             print line
             r.write(line.replace("<mac address=","<mac address1="))
           elif "<source bridge=" in line:
             print line
             r.write(line.replace("<source bridge=","<source bridge1="))
           elif "<target dev" in line:
                 print line
                 r.write(line.replace("<target dev","<target dev1"))
           else :
             print 'no changes'
             continue
#!/usr/bin/python
o = input("enter how many vpcs")
y = input("enter how many eth interfaces")
for k in range(1,o):
        h='vpc'+str(k)+'.xml'
        print h
#j = input("enter how many eth interfaces")
        ri=open(h,'r')
        line = ri.read()
        ri.close()
        for i in range(0,y)
                for s in range(i+1)
        dev_val={'network':'bridge','<man':'<manneth0','<man':'vpc'+str(i)+_eth+str(i),}
                for key,val in dev_val.items():
                                line = line.replace(key,val)
                                print line
                with open(h,'w') as v:
                        v.write(line)
                        v.close()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM