Python结构无法在字符串中找到子字符串
我有两个相同长度的列表。一个包含我要查找的子字符串,一个包含我试图在其中查找子字符串的较长字符串(它将始终位于行的开头)。我的列表包含一个条目,其中子字符串不在字符串中;第二个条目在字符串中包含子字符串 然而,python似乎无法确定子字符串何时存在。我试过很多不同的方法Python结构无法在字符串中找到子字符串,python,string,find,substring,fabric,Python,String,Find,Substring,Fabric,我有两个相同长度的列表。一个包含我要查找的子字符串,一个包含我试图在其中查找子字符串的较长字符串(它将始终位于行的开头)。我的列表包含一个条目,其中子字符串不在字符串中;第二个条目在字符串中包含子字符串 然而,python似乎无法确定子字符串何时存在。我试过很多不同的方法 if substring in string if string.find(substring) != -1 if string.startswith(substring) != -1 前两个“如果” 上面的语句总是返回fa
if substring in string
if string.find(substring) != -1
if string.startswith(substring) != -1
前两个“如果”
上面的语句总是返回false。最后一个“if”语句总是返回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
def agentID():
索引=1
当指数<3时:
ID=linecache.getline('/home/me/project/ID',index)
agentLine=linecache.getline('/home/me/project/agentIDoutput',index)
str(代理行)
如果agentLine.startswith(“%s”%str(ID))!=-1:
打印(“%s:在client.keys”%中找到正确的代理ID%s(env.host\u字符串,ID))
索引=索引+1
其他:
打印(“在第%s行中找不到%s”%(ID,agentLine))
索引=索引+1
这似乎相当简单和直接。我甚至尝试显式转换为字符串,以确保搜索的是同一类型。这就是我认为我的错误所在,但它似乎将它们都解释为字符串 string.startswith()
返回True
或False
,它们实际上分别是整数值1
和0
因此,if string.startswith(substring)!=-1
将始终为真
显然,您的子字符串根本不存在于字符串中。string.startswith()
返回True
或False
,它们实际上分别是整数值1
和0
因此,if string.startswith(substring)!=-1
将始终为真
显然,您的子字符串根本不存在于字符串中。
print
是您的朋友。我尝试了一个快速测试:
>>> import linecache
>>> print(repr(linecache.getline('tmp/testfile.txt', 1)))
'line 1\n'
linecache
提供包括换行符在内的完整行。把它的末端剥掉。你还有几个问题我在这里的时候解决了
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))
print
是你的朋友。我尝试了一个快速测试:
>>> import linecache
>>> print(repr(linecache.getline('tmp/testfile.txt', 1)))
'line 1\n'
linecache
提供包括换行符在内的完整行。把它的末端剥掉。你还有几个问题我在这里的时候解决了
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))
在比较之前,“abcdef”中的“abc”将始终在pythonadd
print(repr(ID)、repr(agentLine))
中工作,以查看要比较的字符串。如下所述,不要比较-1
如果agentLine.startswith(ID)
就足够了。FYI:.getline()
显然已经返回了一个字符串(否则会遇到TypeError
s),所以不需要转换它。但是如果您确实需要,您必须使用agentline=str(agentline)
-您需要重新分配str()的结果
。您会发现行中有“\n”
。执行ID.strip()。如下所述,不要比较-1
如果agentLine.startswith(ID)
就足够了。FYI:.getline()
显然已经返回了一个字符串(否则会遇到TypeError
s),所以不需要转换它。但是如果您确实需要,您必须使用agentline=str(agentline)
-您需要重新分配str()的结果
。您会发现行中有“\n”
。执行ID.strip()
以获得更好的比较。我不知道,因为您所指示的有效!非常感谢。我不知道,因为你说的有用!非常感谢。谢谢我拿出了新行,修改“startswith”是我的问题。我阅读了“find”方法,并假设返回的值(即-1表示false)与“startwith”方法相同。谢谢。我拿出了新行,修改“startswith”是我的问题。我阅读了“find”方法,并假设返回的值(即-1表示false)与“startwith”方法相同。