简体   繁体   English

在字符串中找到东西,然后在python中打印

[英]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值。

Question

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.

相关问题 如果Python的文本文件中没有特定的字符串,如何打印内容? - How to print something if a particular string is not present in a text file in Python? 将工作日保存为变量作为字符串,如果相等则打印“ something” python - Save Weekday into variable as string and if equal print 'something' python 无法在python中的同一行上打印。 既不打印(“ string here”,end =“”),也不打印“东西” - Can not print on same line in python. Neither print(“string here”, end = “”) nor does print “something”, work 在python中查找并打印字符串中的所有数字 - find and print all numbers in a string in python Python重新找到所有组合打印为字符串 - Python re find all combinations print as string 如何在 Python 中将内容打印到控制台? - How to print something to console in Python? Python 在特定日期打印某些内容 - Python print something on a specific day Python如何在字符串中查找子字符串并打印包含子字符串的整个字符串 - Python how to find a substring in a string and print the whole string containing the substring Python:使用re模块查找字符串,然后在字符串下打印值 - Python: Using re module to find string, then print values under string Python:在文本文件中查找字符串并在同一行中的整数&gt; 1时打印它 - Python: Find a string in a text file and print it if the integer in the same line > 1
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM