Posts: 42
Threads: 12
Joined: Jul 2018
I have a STRING and i want to convert that into nested list.
Test-RED# sh run vlan 1-10
!Command: show running-config vlan 1-10
!Time: Thu Aug 9 06:00:45 2018
version 7.1(4)N1(1c)
vlan configuration 1-10
vlan 1
vlan 2
name UNUSED
mode vpc
vlan 10
name SALES
mode vpc
Test-RED#
Test-BLUE# sh run vlan 1-10
!Command: show running-config vlan 1-10
!Time: Thu Aug 9 06:01:37 2018
version 7.1(4)N1(1c)
vlan configuration 1-10
vlan 1
vlan 2
name UNUSED
mode vpc
vlan 10
name HR
mode vpc
Test-BLUE# Expected Output :
Test-RED# = [['vlan 1'], ['vlan 2', ' name UNUSED', ' mode vpc'], ['vlan 10', ' name SALES', ' mode vpc']]
Test-BLUE# = [['vlan 1'], ['vlan 2', ' name UNUSED', ' mode vpc'], ['vlan 10', ' name HR', ' mode vpc']] Basically i want to compare the the diff between Test-RED and Test-BLUE.I know how to compare the nested list but before that i need to convert that into nested list. Any idea how can i do that ?
My current script giving me below output :
[['!Command: show running-config vlan 1-10', '!Time: Thu Aug 9 06:00:45 2018'], ['vlan 10', ' name SALES', ' mode vpc']]
[['!Command: show running-config vlan 1-10', '!Time: Thu Aug 9 06:01:37 2018'], ['vlan 10', ' name HR', ' mode vpc']]
Posts: 4,216
Threads: 97
Joined: Sep 2016
It would help us advise you on how to fix your code if we could see your code. Also, you clearly want to ignore some of the lines and not others. Are the different tests always identified with the octothorpe (#) at the the end of the test name? Are your sub-lists always going to start with 'vlan <number/>'?
Posts: 42
Threads: 12
Joined: Jul 2018
@ ichabod801 : yes your interpretation is perfect.
1. Test-RED or Test-BLUE will always end with "#"
2.Yes , sub-lists always going to start with 'vlan <number>' and end with all elements before another 'vlan <number>' starts.
here is my code :
import re
data_cleaned = {}
current_key = ''
action_flag = False
data_group = []
if_found_vlan = True
output = open('./file1.txt','r').read()
switch_red = re.findall(r'(\w*-RED\d{0,1})', output)[0]
switch_blue = re.findall(r'(\w*-BLUE\d{0,1})', output)[0]
for line in open('./file1.txt'):
m = re.search(r'(\w*-RED\d{0,1}|\w*-BLUE\d{0,1})# sh run vlan \d+', line)
#print m
if m:
if not if_found_vlan:
data_cleaned[current_key].append([])
print current_key
if_found_vlan = False
current_key = m.group(1)
if not data_cleaned.has_key(current_key):
data_cleaned[current_key] = []
continue
mm = re.search(r'vlan \d+', line)
if mm:
if_found_vlan = True
action_flag = True
data_group = []
if action_flag and '' == line.strip():
action_flag = False
data_cleaned[current_key].append(data_group)
if action_flag:
data_group.append(line.replace('\r', '').replace('\n', ''))
if not if_found_vlan:
data_cleaned[current_key].append([])
print data_cleaned[switch_red]
print data_cleaned[switch_blue]file1.txt is the string i had posted earlier .
Posts: 4,216
Threads: 97
Joined: Sep 2016
The problem is that your regex for vlan is catching the 'vlan 1' near the end of the !Command line. Since the vlan's you want are always at the beginning of the line, I would use re.match instead of re.search, because re.match will only check for a match at the beginning of the string.
Also note that using the functions in the re module the way you are means you are compiling the regular expression every time through the loop. It would be much more efficient to get a regular expression object using re.compile. Once you have the compile regex, you can use it's search and match methods much like you would the search and match functions:
vlan_re = re.compile('vlan \d+')
for line in file:
...
if vlan_re.match(line):
...
Posts: 42
Threads: 12
Joined: Jul 2018
I am confused.. so how my code should look like ?
Posts: 4,216
Threads: 97
Joined: Sep 2016
First, change search to match on line 29. Does that fix your problem?
Posts: 42
Threads: 12
Joined: Jul 2018
i changed to match . now its taking the last vlan <number> and the following elements. vlan 1 and vlan 2 and following elements are still missing...
[['vlan 10', ' name SALES', ' mode vpc']]
[['vlan 10', ' name HR', ' mode vpc']]
Posts: 4,216
Threads: 97
Joined: Sep 2016
That's because you are only saving the data group (line 36) when there is a blank line (line 34, note that a blank line is usually tested with not line.strip()). You should be able to fix this by putting a couple of lines after if mm::
if action_flag:
data_cleaned[current_key].append(data_group)That way, if you come into a vlan group right after another vlan group, the previous group gets saved.
Posts: 42
Threads: 12
Joined: Jul 2018
i tried that , i am getting vlan group information of all but its getting repeated.
data_cleaned[switch_red] = [['vlan 1'], ['vlan 2', ' name UNUSED', ' mode vpc'], ['vlan 2', ' name UNUSED', ' mode vpc'], ['vlan 2', ' name UNUSED', ' mode vpc'], ['vlan 10', ' name SALES', ' mode vpc', '', 'Test-RED# ', '', '!Command: show running-config vlan 1-10', '!Time: Thu Aug 9 06:01:37 2018', '', 'version 7.1(4)N1(1c)', 'vlan configuration 1-10'], ['vlan 10', ' name SALES', ' mode vpc', '', 'Test-RED# ', '', '!Command: show running-config vlan 1-10', '!Time: Thu Aug 9 06:01:37 2018', '', 'version 7.1(4)N1(1c)', 'vlan configuration 1-10'], ['vlan 10', ' name SALES', ' mode vpc', '', 'Test-RED# ', '', '!Command: show running-config vlan 1-10', '!Time: Thu Aug 9 06:01:37 2018', '', 'version 7.1(4)N1(1c)', 'vlan configuration 1-10'], ['vlan 10', ' name SALES', ' mode vpc', '', 'Test-RED# ', '', '!Command: show running-config vlan 1-10', '!Time: Thu Aug 9 06:01:37 2018', '', 'version 7.1(4)N1(1c)', 'vlan configuration 1-10'], ['vlan 10', ' name SALES', ' mode vpc', '', 'Test-RED# ', '', '!Command: show running-config vlan 1-10', '!Time: Thu Aug 9 06:01:37 2018', '', 'version 7.1(4)N1(1c)', 'vlan configuration 1-10']]
Posts: 42
Threads: 12
Joined: Jul 2018
|