我需要从HTML文件中将几个数据提取到Excel工作表中。我已经提取了数据,现在我只需要从行中提取数据。这是一个例子:
"501.92secs: iPhone 5s_DownStream HTTP_TCP_Downlink_1 : ILOAD = **12.000** Mbps OLOAD = **4.999** Mbps FRATE = 4.980 Mbps L4 Goodput = 4.788 Mbps Packet Loss = 0.38 SLA Result = **FAIL**<font color=white>"
我粗略地提出了我需要提取的项目。因为这些值随每个文件而变化,并且需要保存为变量名称,所以我真的不知道从哪里开始。
答案 0 :(得分:1)
嗯,这是一个hacky的答案......它看起来不漂亮,但如果你的字符串模式保持不变,你应该能够得到结果。
string = "501.92secs: iPhone 5s_DownStream HTTP_TCP_Downlink_1 : ILOAD = **12.000** Mbps OLOAD = **4.999** Mbps FRATE = 4.980 Mbps L4 Goodput = 4.788 Mbps Packet Loss = 0.38 SLA Result = **FAIL**<font color=white>"
import re
def getnumbers(string,patterns):
results = []
for pattern,number in patterns.items():
match = re.search(pattern,string)
valuestart = match.span()[1]
results.append([pattern,(string[valuestart+3:valuestart+number+3])])
return results
#If you need obtain more values, add them to the dictionary in the argument.
#The number next to each pattern indicates the expected size of the result.
print(getnumbers(string,{"ILOAD =":7,"OLOAD =":7,"Result =":4})) #[['Result =', 'FAIL'], ['OLOAD =', '4.999**'], ['ILOAD =', '12.000*']]