1 回答
TA贡献2041条经验 获得超4个赞
问题通过result += x在每个 if 嵌套中包含也通过添加一些格式来解决,例如'\n' Also,result = ''在 for 循环之前添加以清除变量并防止数据累积,这是最终代码:
import subprocess
jabber = open('C:/Users/Ahmed Mustafa/Desktop/Network Latency Measuring
Tool/output.txt','r')
result = ""
for line in jabber:
if 'statistics' in line.lower():
iploc = line.find("for ") +4
iplocend = line.find(":",iploc)
print(line[iploc:iplocend])
result += (line[iploc:iplocend] + '\n')
if 'packet' in line.lower():
sentloc = line.find("Sent")
sentlocend = line.find(",",sentloc)
print(line[sentloc:sentlocend])
result += (line[sentloc:sentlocend] + '\n')
if 'packet' in line.lower():
rxloc = line.find("Received")
rxlocend = line.find(",",rxloc)
print(line[rxloc:rxlocend])
result += (line[rxloc:rxlocend] + '\n')
if 'packet' in line.lower():
losloc = line.find("Lost")
loslocend = line.find(" (",losloc)
print(line[losloc:loslocend])
result += (line[losloc:loslocend] + '\n')
if 'packet' in line.lower():
lossloc = line.find("(")+1
losslocend = line.find(" loss",lossloc)
print(line[lossloc:losslocend] + " Loss")
result += (line[lossloc:losslocend] + " Loss" + '\n')
if 'minimum' in line.lower():
maxloc = line.find("Maximum")
maxlocend = line.find(",",maxloc)
print(line[maxloc:maxlocend])
result += (line[maxloc:maxlocend] + '\n')
if 'minimum' in line.lower():
minloc = line.find("Minimum")
minlocend = line.find(",",minloc)
print(line[minloc:minlocend])
result += (line[minloc:minlocend] + '\n')
if 'minimum' in line.lower():
avgloc = line.find("Average")
avglocend = line.find(",",avgloc)
print(line[avgloc:avglocend])
result += (line[avgloc:avglocend] + '\n')
print('-'*60)
jabber.close()
with open("outputp.txt", "w") as outputp:
outputp.writelines(str(result))
添加回答
举报