0
import re
def test ( var ):
    op=""" 
    1/1/1/1  up        up :99005      53476      99005      g993-2-17a  
    1/1/1/2  up        up :99005      53148      99005      g993-2-17a  
    1/1/1/3  up        up :99005      53793      99005      g993-2-17a  
    """
    op=op.splitlines()
    for line in op:
        pattern = "([0-9]+/[0-9]+/[0-9]+/[0-9]+) *?([a-z]+) *?([a-z]+) :([0-9]+) +?([0-9]+) +?([0-9]+) +?([a-z0-9-]+)"
        if re.search(pattern, line):
            match=re.search(pattern, line)
            var1=re.sub(r'/', '_', match.group(1))
            x = var+"_"+ var1
            print x
            if_index = match.group(1)
            adm_state = match.group(2)
            exec("global %s" % (x))
            exec("%s = {}" % (x))
            exec("%s['whole']=match.group(0)" % (x))
            exec("%s['if_index']=match.group(1)" % (x))
            exec("%s['adm_state']=match.group(2)" % (x))
            exec("%s['opr_state']=match.group(3)" % (x))
            exec("%s['tx_rate_us']=match.group(5)" % (x))
            exec("%s['tx_rate_ds']=match.group(6)" % (x))
            exec("%s['op_mode']=match.group(7)" % (x))
            print info_1_1_1_1['if_index']


test("info")
print info_1_1_1_1

Hi everyone am new to python and scripting. The above one is my script and my aim is creating multiple dictionary and assigning key and value pair for the corresponding dictionary. For each line i wanted to create separate dictionary. And i wanted to to the dictionary with the same name from global space. If anything not clear let me correct it.

In global space i wanted to access dictionary like info_1_1_1_1['whole']


1 Answer 1

1

global doesn't persist between two exec invocations. This would work:

exec("global bar\nbar=3\n")

But dynamic setting of variables is a strong code smell. Every time you find yourself doing something similar to this, you should immediately stop and reevaluate if there is another way to do this. In this case, I suggest using a dictionary instead:

import re
data = {}
def test ( var ):
    op=""" 
    1/1/1/1  up        up :99005      53476      99005      g993-2-17a  
    1/1/1/2  up        up :99005      53148      99005      g993-2-17a  
    1/1/1/3  up        up :99005      53793      99005      g993-2-17a  
    """
    op=op.splitlines()
    for line in op:
        pattern = "([0-9]+/[0-9]+/[0-9]+/[0-9]+) *?([a-z]+) *?([a-z]+) :([0-9]+) +?([0-9]+) +?([0-9]+) +?([a-z0-9-]+)"
        if re.search(pattern, line):
            match=re.search(pattern, line)
            var1=re.sub(r'/', '_', match.group(1))
            x = var+"_"+ var1
            print(x)
            data[x] = {
                "whole": match.group(0),
                "if_index": match.group(1),
                "adm_state": match.group(2),
                "opr_state": match.group(3),
                "tx_rate_us": match.group(5),
                "tx_rate_ds": match.group(6),
                "op_mode": match.group(7),
            }
            print(data["info_1_1_1_1"]['if_index'])

test("info")
print(data["info_1_1_1_1"])
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.