[英]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. 我从使用snimpy的字符串得到输出。 it looks like this:
它看起来像这样:
ARRIS DOCSIS 3.0 Touchstone WideBand Cable Modem <<HW_REV: 1; VENDOR: Arris Interactive, L.L.C.; BOOTR: 1.2.1.62; 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. 我希望能够查看该字符串并在if中使用该信息,然后打印一些内容。 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.
我想查看该型号是否为CM820A,然后检查固件版本SW_REV,如果它不是正确的版本,则要打印该版本,否则我将移至从循环中获取的下一个字符串。
host.sysDescr it what returns the above string. host.sysDescr它返回上面的字符串。 as of now I know how to find all the CM820A but then i get sloppy when I try to verify the firmware version.
到目前为止,我知道如何查找所有CM820A,但是当我尝试验证固件版本时,我会变得草率。
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. 现在,我能够轻松查看CM820A是否具有正确的版本,但是我无法仅打印坏调制解调器的版本。 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
SW_REV
value. 我只想打印该字符串形式的
SW_REV
值。
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. 我需要有关如何执行此操作的帮助,然后我将更好地理解并且能够重写这整个事情,而我目前仅用于学习python,但我想将其付诸实践以达到有用的目的。
All you need is split()
, you can split your string with a special character for example see the following : 您所需要的只是
split()
,您可以使用特殊字符分割字符串,例如,参见以下内容:
>>> l= s.split(';')
['ARRIS DOCSIS 3.0 Touchstone WideBand Cable Modem <<HW_REV: 1', ' VENDOR: Arris Interactive, L.L.C.', ' BOOTR: 1.2.1.62', ' SW_REV: 7.3.123', ' MODEL: CM820A>>']
>>> for i in l :
... if 'BOOTR' in i:
... print i.split(':')
...
[' BOOTR', ' 1.2.1.62']
So then you can get the second element easily with indexing ! 因此,使用索引可以轻松获得第二个元素!
This answer will simply explain how to retrieve your desired information. 该答案将仅说明如何检索您所需的信息。 You will need to perform multiple splits on your data.
您将需要对数据执行多个拆分 。
First, I notice that your string's information is subdivided by semi-colons. 首先,我注意到您的字符串信息由分号细分。 so:
所以:
description_list = sysdesc.split(";")
will create a list of your major sections. 将创建您的主要部分的列表。 since the sysdesc string has a standard format, you can then access the proper substring:
由于sysdesc字符串具有标准格式,因此您可以访问适当的子字符串:
sub_string = description_list[3]
now, split the substring with the colon: 现在,用冒号分割子字符串:
revision_list = sub_string.split(":")
now, just reference: 现在,请参考:
revision_list[1]
whenever you want to print it. 每当您要打印时。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.