1 回答

TA貢獻(xiàn)2041條經(jīng)驗(yàn) 獲得超4個(gè)贊
問(wèn)題通過(guò)result += x在每個(gè) if 嵌套中包含也通過(guò)添加一些格式來(lái)解決,例如'\n' Also,result = ''在 for 循環(huán)之前添加以清除變量并防止數(shù)據(jù)累積,這是最終代碼:
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))
添加回答
舉報(bào)