Python 从行中提取变量数据
Python Extract variable data from lines
我需要从 HTML 文件中提取几条数据到 excel sheet 中。我已经提取了数据,现在我只需要从行中提取它。这是一个例子:
"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>"
我已经把我需要提取的项目加粗了。因为这些值随每个文件而变化,并且需要将它们保存到变量名中,所以我真的不知道从哪里开始。
好吧,这是一个 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*']]
我需要从 HTML 文件中提取几条数据到 excel sheet 中。我已经提取了数据,现在我只需要从行中提取它。这是一个例子:
"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>"
我已经把我需要提取的项目加粗了。因为这些值随每个文件而变化,并且需要将它们保存到变量名中,所以我真的不知道从哪里开始。
好吧,这是一个 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*']]