Nov-22-2018, 06:26 AM
i am new to Python and i cant get this.I have a List and i want to take the input from there and write those in files .
abc1.txt should have
Any Idea ?
p = ['Eth1/1', 'Eth1/5','Eth2/1', 'Eth2/4','Eth101/1/1', 'Eth101/1/2', 'Eth101/1/3','Eth102/1/1', 'Eth102/1/2', 'Eth102/1/3','Eth103/1/1', 'Eth103/1/2', 'Eth103/1/3','Eth103/1/4','Eth104/1/1', 'Eth104/1/2', 'Eth104/1/3','Eth104/1/4']What i am trying :
with open("abc1.txt", "w+") as fw1, open("abc2.txt", "w+") as fw2: for i in p: if len(i.partition("/")[0]) == 4: fw1.write('int ' + i + '\n mode\n') else: i = 0 while i < len(p): start = p[i].split('/') if (start[0] == 'Eth101'): i += 3 key = start[0] i += 1 while i < len(p) and p[i].split('/')[0] == key: i += 1 end = p[i-1].split('/') fw2.write('confi ' + start[0] + '/' + start[1] + '-' + end[1] + '\n mode\n')What i am looking for :
abc1.txt should have
int Eth1/1 mode int Eth1/5 mode int Eth2/1 mode int Eth 2/4 modeabc2.txt should have :
int Eth101/1/1-3 mode int Eth102/1/1-3 mode int Eth103/1/1-4 mode int Eth104/1/1-4 mode
- So any Eth having 1 digit before " / " ( e:g Eth1/1 or Eth2/2 )should be in one file that is abc1.txt .
- Any Eth having 3 digit before " / " ( e:g Eth101/1/1 or Eth 102/1/1 ) should be in another file that is abc2.txt and .As these are in ranges , need to write it like Eth101/1/1-3, Eth102/1/1-3 etc
Any Idea ?