1

I have 2 lists of identical length. One contains the substrings I want to find, one contains the longer string in which I am trying to find the substring (it will always be at the beginning of the line). The lists I have contain one entry where the substring is not in the string; the second entry has the substring in the string.

However, it looks like python can't figure out when the substring is there or not. I've tried a lot of different ways.

if substring in string
if string.find(substring) != -1
if string.startswith(substring) != -1

The first two "if" statements above always return false. The last "if" statement always returns true.

def agentID():
index = 1
while index < 3:
    ID = linecache.getline('/home/me/project/ID', index)
    agentLine = linecache.getline('/home/me/project/agentIDoutput', index)
    str(agentLine)
    if agentLine.startswith('%s' % str(ID)) != -1:
        print("%s: Proper Agent ID %s found in client.keys" % (env.host_string, ID))
        index = index + 1
    else:
        print("I couldn't find %s in the line %s" % (ID, agentLine))
        index = index + 1 

This seems pretty simple and straightforward. I even tried explicitly converting to strings to ensure I was searching on the same type. That's what I'm thinking my error is, but it seems to interpret them both as strings.

4
  • "abc" in "abcdef" will always work in python Commented Mar 20, 2016 at 15:58
  • add print(repr(ID), repr(agentLine)) before the compare to see what strings you are comparing. And as mentioned below, don't compare for -1. if agentLine.startswith(ID) should suffice. Commented Mar 20, 2016 at 16:03
  • FYI: .getline() obviously already returns a string (otherwise you'd encounter TypeErrors), so you don't need to convert it. But if you did need to, you'd have to use agentline = str(agentline) - you need to reassign the result of str(). Commented Mar 20, 2016 at 16:03
  • You'll find that the lines have "\n" in them. Do ID.strip() to get a better compare. Commented Mar 20, 2016 at 16:05

2 Answers 2

0

string.startswith() returns either True or False which are de facto the integer values 1 and 0, respectively.

Therefore, if string.startswith(substring) != -1 will always be True.

Apparently, your substring simply isn't present in your string.

Sign up to request clarification or add additional context in comments.

1 Comment

I have no idea because what you indicated worked! thank you!
0

print is your friend. I tried a quick test:

>>> import linecache
>>> print(repr(linecache.getline('tmp/testfile.txt', 1)))
'line 1\n'

linecache gives you the full line including newline. Just strip it off of the end. You had a couple of other problems I cleaned up while I was here

def agentID():
    for index in range(1,4):
        ID = linecache.getline('/home/me/project/ID', index).strip()
        agentLine = linecache.getline('/home/me/project/agentIDoutput', index)
        if agentLine.startswith(ID):
            print("%s: Proper Agent ID %s found in client.keys" % (env.host_string, ID))
        else:
            print("I couldn't find %s in the line %s" % (ID, agentLine))

1 Comment

Thanks. I took out the newline and changing correcting the "startswith" was my problem. I read about the "find" method and assuming the values returned (i.e. -1 for false) were the same for the "startwith" method.

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.