-1

So my problem is this: I've been trying to read a certain line from an output for some time now. It's about a router and its sysinfo which I get from a Python script. Now I want to be able to choose from the output lines with information which I get displayed. An output looks like this:

enter image description here

Behind the colons is the information. For example, if I only want to have the name, I want to have "nameofdevice" output when I enter "DEVICE" as the parameter.

I have already seen after extensive research that you can do something like this with so-called Dictionarys my question is now how I can implement this exactly. To change the output then. I am grateful for every pseudocode!

Edit: It is no duplicate i dont want to use lists i want to use dictionarys...

Jaexz
  • 33
  • 7
  • Its no duplicate i dont want to read each line and i dont work with lists this is a dictionary and has nothing to do with a list. Look it up: List: https://www.w3schools.com/python/python_lists.asp Dictionary: https://www.w3schools.com/python/python_dictionaries.asp – Jaexz Mar 22 '19 at 08:11
  • What "output" are you talking about? Is it a console output in bash or Windows Command?... If this data is coming froma Python Script, why don't you change the script so that it behaves like you want it? – Sven-Eric Krüger Mar 22 '19 at 08:33

1 Answers1

2

The more python coding you do the more you will come to love dictionaries. They are the most useful data structure.

Something like this code will load the file and for each line in the file split it into two parts - the first is the key into the dictionary - which is how you reference it, the second is the value that the dictionary is storing for that key.

data = {}
with open('textfile') as fh:
    for line in fh:
        key, value = line.strip().split(':', 1)
        data[key] = value

Now you should be able to access the data through something like:

print(data['NAME'])
dwagon
  • 501
  • 3
  • 9
  • Is it possible to do this without using a textfile and filtering the data before it gets saved in a textfile? And thanks for your help! – Jaexz Mar 22 '19 at 08:08
  • "dictionary[key] = value" is the bit that updates the dictionary. You can change the value all you like before hand (or even after). Same with putting conditions on it. – dwagon Mar 22 '19 at 08:23