我们说我有一个这样的字符串:
Benchmark\r\n\tRunning for engine innodb\r\n\tAverage number of seconds to run all queries: 0.374 seconds\r\n\tMinimum number of seconds to run all queries: 0.374 seconds\r\n\tMaximum number of seconds to run all queries: 0.374 seconds\r\n\tNumber of clients running queries: 1\r\n\tAverage number of queries per client: 2
如果我只对值后跟“平均运行所有查询的秒数”感兴趣:',我该如何获取?
答案 0 :(得分:1)
这个问题的一个难题是使用正则表达式,因为正则表达式是处理模式匹配的一般方法。
例如,您可以使用以下代码在此处解决您的问题:
import re
mystring = 'Your input'
float_pattern = '(\d*\.\d*)'
prefix = 'Average number of seconds to run all queries: '
postfix = ' seconds'
all_time = re.findall(prefix+float_pattern+postfix,mystring)
all_time_t = map(lambda x:float(x),all_time)
average_time = sum(all_time_t)/len(all_time_t)
阅读正则表达式文档以获取更多信息:https://docs.python.org/2/library/re.html
答案 1 :(得分:1)
您可以split()
字符串,并将值添加到字典中。然后,您可以根据需要访问所有值。
a = Benchmark\r\n\tRunning for engine innodb\r\n\tAverage number of seconds to run all queries: 0.374 seconds\r\n\tMinimum number of seconds to run all queries: 0.374 seconds\r\n\tMaximum number of seconds to run all queries: 0.374 seconds\r\n\tNumber of clients running queries: 1\r\n\tAverage number of queries per client: 2
lst = a.split("k\r\n\")
d = {}
for i in z[2:]:
k,v = i.split(":")
d[k] = v
或者
d = dict((k,v) for k,v in (i.split(':') for i in a.split("\r\n\t")[2:]))
现在,您可以通过执行
来访问所有值d["Average number of seconds to run all queries"]
>>>' 0.374 seconds'
答案 2 :(得分:0)
我的解决方案非常特定于您提供的数据,并且不会很好地概括,但在此您是:
import re
teststring="Benchmark\r\n\tRunning for engine innodb\r\n\tAverage number of seconds to run all queries: 0.374 seconds\r\n\tMinimum number of seconds to run all queries: 0.374 seconds\r\n\tMaximum number of seconds to run all queries: 0.374 seconds\r\n\tNumber of clients running queries: 1\r\n\tAverage number of queries per client: 2"
prog = re.compile(".*Average number of seconds to run all queries: (\d+\.\d+) seconds.*", re.DOTALL)
result = prog.match(teststring)
print result.group(1)
输出:
macbook:Downloads joeyoung$ python regextest.py
0.374