0

I am having issues passing a string variable into a search function.

Here is what I'm trying to accomplish:
I have a file full of values and I want to check the file to make sure a specific matching line exists before I proceed. I want to ensure that the line <endSW=UNIQUE-DNS-NAME-HERE<> exists if a valid <begSW=UNIQUE-DNS-NAME-HERE<> exists and is reachable.

Everything works fine until I call if searchForString(searchString,fileLoc): which always returns false. If I assign the variable 'searchString' a direct value and pass it it works, so I know it must be something with the way I'm combining the strings, but I can't seem to figure out what I'm doing wrong.

If I examine the data that 'searchForString' is using I see what seems to be valid values:

values in fileLines list:

['<begSW=UNIQUE-DNS-NAME-HERE<>', '    <begPortType=UNIQUE-PORT-HERE<>', '        <portNumbers=80,443,22<>', '    <endPortType=UNIQUE-PORT-HERE<>', '<endSW=UNIQUE-DNS-NAME-HERE<>']

value of searchVar:

<endSW=UNIQUE-DNS-NAME-HERE<>

An example of the entry in the file is:

<begSW=UNIQUE-DNS-NAME-HERE<>
    <begPortType=UNIQUE-PORT-HERE<>
        <portNumbers=80,443,22<>
    <endPortType=UNIQUE-PORT-HERE<>
<endSW=UNIQUE-DNS-NAME-HERE<>

Here is the code in question:

def searchForString(searchVar,readFile):
    with open(readFile) as findMe:
        fileLines = findMe.read().splitlines()
        print fileLines
        print searchVar
        if searchVar in fileLines:
            return True
        return False
    findMe.close()

fileLoc = '/dir/folder/file'
fileLoc.lstrip()
fileLoc.rstrip()
with open(fileLoc,'r') as switchFile:
    for line in switchFile:
        #declare all the vars we need
        lineDelimiter = '#'
        endLine = '<>\n'
        begSWLine= '<begSW='
        endSWLine = '<endSW='
        begPortType = '<begPortType='
        endPortType = '<endPortType='
        portNumList = '<portNumbers='
        #skip over commented lines -(REMOVE THIS)
        if line.startswith(lineDelimiter):
            pass
        #checks the file for a valid switch name
        #checks to see if the host is up and reachable
        #checks to see if there is a file input is valid
        if line.startswith(begSWLine):
            #extract switch name from file
            switchName = line[7:-3]
            #check to make sure switch is up
            if pingCheck(switchName):
                print 'Ping success. Host is reachable.'
                searchString = endSWLine+switchName+'<>'
                **#THIS PART IS SUCKING, WORKS WITH DIRECT STRING PASS
                #WONT WORK WITH A VARIABLE**
                if searchForString(searchString,fileLoc):
                    print 'found!'
                else:
                    print 'not found'   

Any advice or guidance would be extremely helpful.

1
  • After doing some more reading I've decided to change the input text file to XML and use the built in libraries. Commented Dec 7, 2016 at 18:18

2 Answers 2

0

Hard to tell without the file's contents, but I would try

switchName = line[7:-2]

So that would look like

>>> '<begSW=UNIQUE-DNS-NAME-HERE<>'[7:-2]
'UNIQUE-DNS-NAME-HERE'

Additionally, you could look into regex searches to make your cleanup more versatile.

import re
# re.findall(search_expression, string_to_search)   

>>> re.findall('\=(.+)(?:\<)', '<begSW=UNIQUE-DNS-NAME-HERE<>')[0]
'UNIQUE-DNS-NAME-HERE'

>>> e.findall('\=(.+)(?:\<)', '        <portNumbers=80,443,22<>')[0]
'80,443,22'
Sign up to request clarification or add additional context in comments.

2 Comments

Here's an example of the contents of the file: <begSW=US-CA-SD-190IN4-BA01<> <begPortType=GigabitEthernet<> <portNumbers=1/0/1,1/0/3,4/0/8,5/0/12,2/0/7<> <endPortType=GigabitEthernet<> <endSW=US-CA-SD-190N4-BA01<>
Make sure you're getting an array like ['<begSW=US-CA-SD-190IN4-BA01<>', '<begPortType=GigabitEthernet<>', '<portNumbers=1/0/1,1/0/3,4/0/8,5/0/12,2/0/7<>', '<endPortType=GigabitEthernet<>', '<endSW=US-CA-SD-190N4-BA01<>'] Without that, the 'for line in switchFile:' won't work
0

I found how to recursively iterate over XML tags in Python using ElementTree? and used the methods detailed to parse an XML file instead of using a TXT file.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.