Python Forum
filter just with the string word
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
filter just with the string word
#1
Do anyone know to to just print the string after query from mib?
After running the script will show "Test:SNMPv2-SMI::mib-2.69.1.4.5.0 = STRING: "test.cfg"", how to left out with just "test.cfg"


import os
direct_output = os.popen('snmpwalk -cpublic -v 2c 172.16.25.42 1.3.6.1.2.1.69.1.4.5.0 ').read()
print("File Name:"+direct_output)

#output: Test:SNMPv2-SMI::mib-2.69.1.4.5.0 = STRING: "test.cfg"

if direct_output =="test.cfg":
    print('Same File name')
print("end script')

i used this way:
import os

direct_output = os.popen('snmpwalk -cpublic -v 2c 172.16.25.42 1.3.6.1.2.1.69.1.4.5.0 ').read()
#print("Test:"+direct_output)
test123=direct_output.replace('SNMPv2-SMI::mib-2.69.1.4.5.0 = STRING: ','')
print(test123)
if test123 == "test.cfg":
    print("same")
else:

    print("end")
but it didn't print same, do anyon know why

i saw the problem to my code, one have double quote " ", another one have no quote
"test.cfg"
test.cfg

any way to remove the " "
Reply
#2
HI All,
i have solved all the problem, but i know this code is not good, is there better way of writing it.
Thanks.

import os

direct_output = os.popen('snmpwalk -cpublic -v 2c 172.16.25.42 1.3.6.1.2.1.69.1.4.5.0 ').read()
test123=direct_output.replace('SNMPv2-SMI::mib-2.69.1.4.5.0 = STRING: ','')
test123=test123.replace('"','')
test123=test123.replace('\n','')


if test123 == "test.cfg":
    print("same")
else: 
    print("end")
Reply
#3
Could write a regex for this,so here is the result the same if no quote or if there is singel/double quote.
>>> import re 
>>> 
>>> s = 'Test:SNMPv2-SMI::mib-2.69.1.4.5.0 = STRING: "test.cfg"'
>>> r = re.search(r':\s(.*)', s).group(1)
>>> r = re.sub(r'\'|\"', '', r)
>>> r
'test.cfg'
>>> 
>>> s = 'Test:SNMPv2-SMI::mib-2.69.1.4.5.0 = STRING: test.cfg'
>>> r = re.search(r':\s(.*)', s).group(1)
>>> r = re.sub(r'\'|\"', '', r)
>>> r
'test.cfg'
>>> 
>>> s = "Test:SNMPv2-SMI::mib-2.69.1.4.5.0 = STRING: 'test.cfg'"
>>> r = re.search(r':\s(.*)', s).group(1)
>>> r = re.sub(r'\'|\"', '', r)
>>> r
'test.cfg'
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Retrieve word from string knob 4 432 Jan-22-2024, 06:40 PM
Last Post: Pedroski55
  extract substring from a string before a word !! evilcode1 3 491 Nov-08-2023, 12:18 AM
Last Post: evilcode1
  Isolate a word from a long string nicocorico 2 1,501 Feb-25-2022, 01:12 PM
Last Post: nicocorico
  change string in MS word Mr_Blue 8 3,218 Sep-19-2021, 02:13 PM
Last Post: snippsat
Question Problem: Check if a list contains a word and then continue with the next word Mangono 2 2,455 Aug-12-2021, 04:25 PM
Last Post: palladium
  Python Speech recognition, word by word AceScottie 6 15,862 Apr-12-2020, 09:50 AM
Last Post: vinayakdhage
  Converting query string as a condition for filter data. shah_entrance 1 1,752 Jan-14-2020, 09:22 AM
Last Post: perfringo
  I want to filter out words with one letter in a string (pig latin translator) po0te 1 2,065 Jan-08-2020, 08:02 AM
Last Post: perfringo
  Reverse the string word sneha 2 2,569 Dec-12-2019, 03:37 AM
Last Post: sneha
  Cannot Remove the Double Quotes on a Certain Word (String) Python BeautifulSoup soothsayerpg 5 6,991 Oct-27-2019, 09:53 AM
Last Post: newbieAuggie2019

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020