Posts: 42
Threads: 12
Joined: Jul 2018
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 .
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
mode abc2.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 ?
Posts: 2,953
Threads: 48
Joined: Sep 2016
You should get an error because of line 2. The indentation is wrong.
with open("abc1.txt", "w+") as fw1, open("abc2.txt", "w+") as fw2:
for element in p:
if len(element.strip('Eth').split('/')[0]) == 1: # if you have one digit before the first /
# write to abc1.txt
else: # if you a sure that the input is correct else should work if you have three digits before /
# write to abc2.txt Or you can use regular expressions. I am not good at it. Never needed it so far :D
Posts: 42
Threads: 12
Joined: Jul 2018
Hey , i tried below
p = ['Eth1/1', 'Eth1/48','Eth2/1', 'Eth2/4','Eth101/1/1', 'Eth101/1/16', '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']
with open("abc1.txt", "w+") as fw1, open("abc2.txt", "w+") as fw2:
for element in p:
if len(element.strip('Eth').split('/')[0]) == 1: # if you have one digit before the first /
fw1.write('int ' + i + '\n mode\n')
# write to abc1.txt
else: # if you a sure that the input is correct else should work if you have three digits before /
# write to abc2.txt
fw2.write('int ' + i + '\n mode\n') both the file abc1.txt and abc2.txt contains
int Eth104/1/4
mode
int Eth104/1/4
mode
int Eth104/1/4
mode
int Eth104/1/4
mode
Posts: 2,953
Threads: 48
Joined: Sep 2016
Are you sure that the files are not empty?
This is what I am trying - Python 3.5.2:
In [1]: cd tmp/
/home/victor/tmp
In [2]: 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']
In [3]: with open('abc1.txt', 'w+') as fw1, open('abc2.txt', 'w+') as fw2:
...: for element in p:
...: if len(element.strip('Eth').split('/')[0]) == 1:
...: fw1.write('int {}\n mode\n'.format(element))
...: else:
...: fw2.write('int {}\n mode\n'.format(element))
...:
In [4]: This is what is in the files:
In [4]: cat abc1.txt
int Eth1/1
mode
int Eth1/5
mode
int Eth2/1
mode
int Eth2/4
mode
In [5]: cat abc2.txt
int Eth101/1/1
mode
int Eth101/1/2
mode
int Eth101/1/3
mode
int Eth102/1/1
mode
int Eth102/1/2
mode
int Eth102/1/3
mode
int Eth103/1/1
mode
int Eth103/1/2
mode
int Eth103/1/3
mode
int Eth103/1/4
mode
int Eth104/1/1
mode
int Eth104/1/2
mode
int Eth104/1/3
mode
int Eth104/1/4
mode I don't know how you determine the ranges.
Posts: 42
Threads: 12
Joined: Jul 2018
This one i am also getting but i need to define the range in abc2.txt
101/1/1-3
102/1/1-3
103/1/1-4
104/1/1-4
Posts: 2,953
Threads: 48
Joined: Sep 2016
Posts: 42
Threads: 12
Joined: Jul 2018
Nov-22-2018, 11:08 AM
(This post was last modified: Nov-22-2018, 11:08 AM by Nirmal.)
i have a script and i think we can achieve the objective but i am missing somewhere ?
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('/')
print start
if (start[0] == 'Eth1'):
i += 3
else:
key = start[0]
#print key
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')
Posts: 4,783
Threads: 76
Joined: Jan 2018
Nov-22-2018, 12:29 PM
(This post was last modified: Nov-22-2018, 12:29 PM by Gribouillis.)
Itertools can help for this task
import io
import itertools as itt
def make_ranges(ints):
ii = iter(sorted(ints))
a = b = next(ii)
for k in ii:
if k == b + 1:
b = k
else:
yield a, b
a = b = k
yield a, b
p = ['Eth1/1', 'Eth1/48','Eth2/1', 'Eth2/4','Eth101/1/1', 'Eth101/1/16', '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']
p = [x.split('/') for x in p]
p = [(x[0],) + tuple(int(y) for y in x[1:]) for x in p]
q, r = [sorted(g) for k, g in itt.groupby(sorted(p, key=len), key=len)]
fw1 = io.StringIO()
for rec in q:
print('/'.join(str(x) for x in rec), file=fw1)
fw2 = io.StringIO()
for k, g in itt.groupby(r, key=lambda x: x[:-1]):
for a, b in make_ranges(int(x[-1]) for x in g):
kk = k + (a if a == b else '{}-{}'.format(a, b),)
print('/'.join(str(x) for x in kk), file=fw2)
print(fw1.getvalue())
print(fw2.getvalue()) Output: Eth1/1
Eth1/48
Eth2/1
Eth2/4
Eth101/1/1
Eth101/1/3
Eth101/1/16
Eth102/1/1-3
Eth103/1/1-4
Eth104/1/1-4
Posts: 42
Threads: 12
Joined: Jul 2018
its saying "invalid Syntax " -- print('/'.join(str(x) for x in rec), file=fw1)
Posts: 4,783
Threads: 76
Joined: Jan 2018
Nov-23-2018, 07:37 AM
(This post was last modified: Nov-23-2018, 07:37 AM by Gribouillis.)
Nirmal Wrote:its saying "invalid Syntax " -- When I run the exact code I posted above with python 3.5, it runs without error. I now always write python 3 code. Are you still using python 2.7? In that case, add this as the very top of your script
# -*-coding: utf8-*-
from __future__ import (absolute_import, division,
print_function, unicode_literals)
|