0

I would like to get specific data values based on a match for a specific ID which is not sorted and could be anywhere in the line. Here is an example:

[ROUTE:10:23:191:271:3:81]
     route_protocol = ripv2
     dhcp_enabled = yes
     route_config = dynamic

How can I get the route_protocol value only for ROUTE IDs: 23 and 81?

9
  • You could use a str method, .split(':'), and grab the values at the indices you want. Commented Jun 9, 2017 at 19:33
  • Can you show an example? Commented Jun 9, 2017 at 19:33
  • Is this an INI file? Commented Jun 9, 2017 at 19:36
  • 1
    Please show a more complete example and expected output. Commented Jun 9, 2017 at 19:37
  • Hi Jon, the file is a text file with a config on that format. So I need to create a regular expression that: 1) find the [ROUTE\: 2) then the ID (for example: 23) 3) then if that is found, I want to get the route_protocol = data [ROUTE:{ID}] value = data Commented Jun 9, 2017 at 19:38

1 Answer 1

1

This will return the values in a list in order of appearance.

with open('conftxt.txt') as config:
    l = [x for x in config.readlines() if 'route_config' in x]
    route_config_vals = [x.strip().split(' = ')[1] for x in l]

print(route_config_vals) #['dynamic']

EDIT

def extract_values(param: "str paramaeter to search for",
                   ) -> "dict:  ROUTE: param pairs":


  with open('conftxt.txt') as config:
        parameter_list = []

        # get list of parameters
        l = [x for x in config.readlines() if param in x]
        parameter_list= [x.strip().split(' = ')[1] for x in l]

        config.seek(0)  #back to top of file

        # get list of routes
        route_list = [x.strip() for x in config.readlines() if 'ROUTE' in x]
        pairs = {k: v for k, v in zip(route_list, parameter_list)}
        return pairs


print(extract_values('route_config'))  #{'[ROUTE:10:23:191:271:3:81]': dynamic'}
print(extract_values('dhcp_enabled'))  #{'[ROUTE:10:23:191:271:3:81]': 'yes'}
Sign up to request clarification or add additional context in comments.

5 Comments

I will try this :) Thank you!
No prob. Are you wanting the route in squared brackets to be mapped to it's route_config value?
The thing with the file is that there can be other route_configs: [ROUTE:10:23:191:271:3:81] route_protocol = ripv2 dhcp_enabled = yes route_config = dynamic [ROUTE:99:123] ` route_protocol = ospf` dhcp_enabled = no route_config = dynamic
My goal is to be able to say: print "Network {} is configured as: {}".format(id, route_config)
Check my edit, Its' designed so you can specify a parameter to search for an it will iterate through the file and make a dictionary of route: parameter_value pairs. You can then just iterate through the dictionary and do what you need with it.

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.