本问题已经有最佳答案,请猛点这里访问。
我试图在Python中的较大字符串中找到一个子字符串。我试图找到字符串"每秒请求数:"后的文本。似乎我对python字符串和python的一般知识是缺乏的。
我的错误是在代码的第3行minusStuffBeforeReqPer = output[reqPerIndx[0], len(output)]上,我得到了这样的错误:没有[0]在reqperindx上,我试图访问一个元组,但有了它,我得到了错误,我int object has no attribute __getitem__。我试图在output字符串中找到reqperstr开头的索引。
代码
#output contains the string reqPerStr.
reqPerStr ="Requests per second:"
reqPerIndx = output.find(reqPerStr)
minusStuffBeforeReqPer = output[reqPerIndx[0], len(output)]
eolIndx = minusStuffBeforeReqPer.find("
")
semiColIndx = minusStuffBeforeReqPer.find(":")
instanceTestObj.reqPerSec = minusStuffBeforeReqPer[semiColIndx+1, eolIndx]
我觉得这不是最好的方法。如果要查找出现在已知子字符串之后的子字符串,则应使用regex lookbehinds。
find()方法返回一个表示索引的整数。您正在尝试reqperindx[0],这没有意义。
如果您在本页中查看问题的右侧,您将看到一列相关问题。他们中的一些人有你想要的答案。当你写问题的时候,同样的列表也会出现。
您必须使用output[begin:end],而不是output[begin, end](这正是划分普通字符串/列表等的语法的工作方式)。所以:
minusStuffBeforeReqPer = output[reqPerIndx:len(output)]
然而,这是多余的。所以你应该做的是:
minusStuffBeforeReqPer = output[reqPerIndx:]
通过省略片的end部分,片将一直延伸到output的末端。
在没有[0]的情况下访问一个元组会出错,因为您已经将一个元组(即(reqPerIndx, len(output))传递给切片的[...],而在没有__getitem__的情况下访问int会出错,因为在编写reqPerIndx[0]时,您试图获取reqPerIndx的0第个元素,这是一个整数,但是当然,没有"整数的第0个元素"这样的东西,因为整数没有元素。
正如@ashwinichaudhary在评论中指出的那样,如果没有找到子字符串,str.find将返回-1。如果你确信你要找的东西总是在output的某个地方找到,我想你不需要处理-1案件,但无论如何这样做可能是个好主意。
reqPerIndx = output.find(reqPerStr)
if reqPerIndx != -1:
minusStuffBeforeReqPer = ...
# etc
else:
# handle this case separately
你可能对正则表达式有更好的运气。我不知道output是什么样子,所以我猜你应该调整它来匹配你在output中拥有的东西。
>>> import re
>>> re.findall(r'(?:Requests per second:)\s*(\d+)',"Requests: 24")
[]
>>> re.findall(r'(?:Requests per second:)\s*(\d+)',"Requests per second: 24")
['24']
注意,对于缺少的子字符串,str.find返回-1,这也应该处理。
@Ashwinichaudhary说,谢谢。
我从来没想过用regex,我对它不是很精通。但是给定的regex代码,它是如何读取的(find string"and return….)?
看看regex101.com/r/ax9yi6-它可能会有所帮助。基本上,(?:...)的意思是"寻找...但不捕获它(即返回到输出中)"。\s*的意思是"寻找任何数量的空白"。最后,(\d+)的意思是"查找一个或多个数字,并捕获它(即返回到输出中)"。
这两行有错误:
minusStuffBeforeReqPer = output[reqPerIndx[0], len(output)]
instanceTestObj.reqPerSec = minusStuffBeforeReqPer[semiColIndx+1, eolIndx]
您必须使用:创建一个范围。start:end。
可以省略最后一个参数以到达结尾,也可以省略第一个参数以省略开头。参数也可以是负数。由于find可能返回-1,因此您必须以不同的方式处理它,因为如果找不到字符串,您将得到:
minusStuffBeforeReqPer = output[-1:]
这是字符串中的最后一个字符。
您应该有这样的代码:
#output contains the string reqPerStr.
reqPerStr ="Requests per second:"
reqPerIndx = output.find(reqPerStr)
if reqPerIndx != -1:
minusStuffBeforeReqPer = output[reqPerIndx[0]:]
eolIndx = minusStuffBeforeReqPer.find("
")
semiColIndx = minusStuffBeforeReqPer.find(":")
if eolIndx > semiColIndx >= 0:
instanceTestObj.reqPerSec = minusStuffBeforeReqPer[semiColIndx+1:eolIndx]
这很好,但是,您必须用regex更改代码。据我所知,你真的想匹配一个以reqPerStr开头,以结尾的字符串,得到:和之间的所有内容。
你可以用这样的模式来做到:
"Requests per second:(.*)
"
你最终会得到:
import re
reqPerIndx = output.find(reqPerStr)
match = re.match("Requests per second:(.*)
", output)
if match:
instanceTestObj.reqPerSec = match.group(1)
如果要查找所有匹配项,可以执行以下操作:
for match in re.finditer("Requests per second:(.*)", output)
instanceTestObj.reqPerSec = match.group(1)