Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Key Error in Python
#1
The below script i have to compare Test-red vs Test-blue and find the difference. .Test-red and Test-blue are kept in "output.txt" . output.txt content is mentioned in the bottom.

import re
    data_cleaned = {}
    current_key = ''
    action_flag = False
    data_group = []
    if_found_vlan = True

    output = open('./output.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('./output.txt'):
        m = re.match(r'(\w*-RED\d{0,1}|\w*-BLUE\d{0,1})# sh run vlan \d+', line)

        if m:
            if not if_found_vlan:
                data_cleaned[current_key].append([])
            if_found_vlan = False

            current_key = m.group(1)
            if not data_cleaned.has_key(current_key):
                data_cleaned[current_key] = []
            continue

        mm = re.match(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 ("+++++++++++++++++ The missing configuration ++++++++++++++\n")
    print switch_blue + "#" + " has below missing VLAN config\n "
    p = [item for index, item in enumerate(data_cleaned[switch_blue]) if [] != [it for it in item if it not in data_cleaned[switch_red][index]]]
    print('\n'.join(['\n'.join(item) for item in p]))
    print ("+++++++++++++++++++++++++++++++\n")
    print switch_red + "#" + " has below missing VLAN config\n "
    q = [item for index, item in enumerate(data_cleaned[switch_red]) if [] != [it for it in item if it not in data_cleaned[switch_blue][index]]]
    print('\n'.join(['\n'.join(item) for item in q]))
output.txt is :

Test-red# sh run vlan 158

!Command: show running-config vlan 158
!Time: Mon Jul 23 08:34:49 2018

version 7.1(4)N1(1c)
vlan configuration 158
vlan 158
  name MARKETING
  mode fabricpath

Test-red# sh run vlan 159

!Command: show running-config vlan 159
!Time: Mon Jul 23 08:34:53 2018

version 7.1(4)N1(1c)
vlan configuration 159
vlan 159


Test-red#
------------------------------------
Test-blue# sh run vlan 158

!Command: show running-config vlan 158
!Time: Mon Jul 23 08:35:37 2018

version 7.1(4)N1(1c)
vlan configuration 158
vlan 158
  name MARKETING
  mode fabricpath

Test-blue# sh run vlan 159

!Command: show running-config vlan 159
!Time: Mon Jul 23 08:35:42 2018

version 7.1(4)N1(1c)
vlan configuration 159
vlan 159
  name SALES
  mode fabricpath


Test-blue# 
i am getting below error:
p = [item for index, item in enumerate(data_cleaned[switch_blue]) if [] != [it for it in item if it not in data_cleaned[switch_red][index]]]
KeyError: 'Test-blue'
Any idea how to fix it up ?
Reply
#2
There is no 'Test-blue' key in data_cleaned.
I have to say that the code is hard to read.
The list comprehension is a convenient way to make lists but not sacrificing the readability of the code. Nested list comprehensions?!
See how to use generators.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#3
You are looking for uppercase red and blue. Use r'(?i)' prefix to ignore case - colour words in your text are lower-case, and you specify uppercase pattern.

\d{0,1} is rather clumsy construct - \d? will do the same in 4 less symbol
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply
#4
@volcano63 : i corrected 'output.txt' ( i.e RED and BLUE are in upper case ). thing is now when i use only that script , its working fine .but when i integrated this with other function its giving me "key error".Please find the complete script ( after integration). dont know where its going wrong.it giving "KeyError: 'Test-blue'" .could you please suggest ?
Reply
#5
If it is line 97 (full traceback Wall ) - data_cleaned may be an empty dictionary

I am not sure what you are trying to achieve - your code seems over-complicated, but that may be the reason.
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply
#6
data_cleaned is missing with the key Test-BLUE . but i see "Test-RED " thats why its saying "key error-Test-BLUE".there are two keys here . Test-RED and Test-BLUE but Test-BLUE is missing from dictionary.i dont know why ?
Reply
#7
You need to simplify the code.

Use sys.exit instead of just exit. The last one is intended for the interpreter.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#8
Take your data_parsing function, insert prints of intermediate results - and you'll be able to understand what you code is doing. Just run this code with ready-made data file.
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply
#9
When i run the code with ready-made file it works fine..no issue with that....when i run the complete script that also gives the file same as ready-made file...after that what happens , no idea.. i did all prints.. got to know things all getting changed here:

if action_flag and '' == line.strip():
        action_flag = False
        data_cleaned[current_key].append(data_group)
        print data_cleaned
no idea whts going wrong here.completly lost.

i changed exit() to sys.exit
Reply
#10
I made a small experiment with your data (I use Python3)

Output:
In [50]: color_keys = set(re.findall(r'(?i)\w+-(?:red|blue)\d?', data)) ...: key_re = re.compile(r'({}|{})# sh run vlan \d+'.format(*color_keys)) ...: for line in data.splitlines(): ...: key_match = key_re.match(line) ...: if key_match: ...: current_key = key_match.group(1) ...: print(current_key) ...: else: ...: vlan_match = re.match(r'vlan \d+', line) ...: if vlan_match: ...: print(vlan_match.group(0)) ...: Test-red vlan 158 Test-red vlan 159 Test-blue vlan 158 Test-blue vlan 159
I just looked up the data. Now, decide how do you want to group that data. Write down what you expect. Forget other parts of the code - concentrate on making it work. Start with deciding what do you want to get after the loop.

After you get the data you want - proceed from there. You know which result you want to get - works towards that.
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020