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.
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..getline()obviously already returns a string (otherwise you'd encounterTypeErrors), so you don't need to convert it. But if you did need to, you'd have to useagentline = str(agentline)- you need to reassign the result ofstr()."\n"in them. DoID.strip()to get a better compare.