Jason Kack Jason Kack - 1 month ago 4x
Python Question

find something in a string then print it in python

im sure this is simple but im not good with regexp or string manipulation and i want to learn :)

I have an output from a string I get using snimpy. it looks like this:

ARRIS DOCSIS 3.0 Touchstone WideBand Cable Modem <<HW_REV: 1; VENDOR: Arris Interactive, L.L.C.; BOOTR:; SW_REV: 7.3.123; MODEL: CM820A>>

I want to be able to look into that string and use that info in an if to then print some stuff. I want to see if the model is a CM820A and then check the firmware version SW_REV and if its not the right version I want to print the version else I move on to the next string i get from my loop.

host.sysDescr it what returns the above string. as of now I know how to find all the CM820A but then i get sloppy when I try to verify the firmware version.

sysdesc = host.sysDescr
if "CM820A" in str(sysdesc):
if "7.5.125" not in str(sysdesc):
print("Modem CM820A " + modem + " at version " + version)
print(" Sysdesc = " + sysdesc)
if "7.5.125" in sysdesc:
print ("Modem CM820A " + modem + " up to date")

Right now I am able to see if the CM820A has the right version easily but I can't print only the version of the bad modems. I was only able to print the whole string which contains a lot of useless info. I just want to print form that string the


I need help with how to do this then I will understand better and be able to rewrite this whole thing which I currently am using only to learn python but I want to put to practice for useful purposes.


All you need is split() , you can split your string with a special character for example see the following :

>>> l= s.split(';')
['ARRIS DOCSIS 3.0 Touchstone WideBand Cable Modem <<HW_REV: 1', ' VENDOR: Arris Interactive, L.L.C.', ' BOOTR:', ' SW_REV: 7.3.123', ' MODEL: CM820A>>']

>>> for i in l :
...  if 'BOOTR' in i:
...   print i.split(':')
[' BOOTR', '']

So then you can get the second element easily with indexing !