第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

將循環(huán)輸出的完整轉(zhuǎn)儲(chǔ)到 Python 中的文本文件

將循環(huán)輸出的完整轉(zhuǎn)儲(chǔ)到 Python 中的文本文件

我正在運(yùn)行 for 循環(huán)以讀取文件 (txt) 并將文件中的信息切片。切片后,我試圖將整個(gè)結(jié)果轉(zhuǎn)儲(chǔ)到文本文件中,代碼如下:import subprocessjabber = open('C:/Users/Ahmed Mustafa/Desktop/Network Latency Measuring Tool/output.txt','r')for line in jabber:    if 'statistics' in line.lower():        iploc = line.find("for ") +4        iplocend = line.find(":",iploc)        print(line[iploc:iplocend])    if 'packet' in line.lower():        sentloc = line.find("Sent")        sentlocend = line.find(",",sentloc)        print(line[sentloc:sentlocend])    if 'packet' in line.lower():        rxloc = line.find("Received")        rxlocend = line.find(",",rxloc)        print(line[rxloc:rxlocend])    if 'packet' in line.lower():        losloc = line.find("Lost")        loslocend = line.find(" (",losloc)        print(line[losloc:loslocend])    if 'packet' in line.lower():        lossloc = line.find("(")+1        losslocend = line.find(" loss",lossloc)        print(line[lossloc:losslocend] + " Loss")    if 'minimum' in line.lower():        maxloc = line.find("Maximum")        maxlocend = line.find(",",maxloc)        print(line[maxloc:maxlocend])    if 'minimum' in line.lower():        minloc = line.find("Minimum")        minlocend = line.find(",",minloc)        print(line[minloc:minlocend])        result = line[minloc:minlocend]    if 'minimum' in line.lower():        avgloc = line.find("Average")        avglocend = line.find(",",avgloc)        print(line[avgloc:avglocend])        result = line[avgloc:avglocend]        print('-'*60)jabber.close()with open("outputp.txt", "w") as outputp:    outputp.writelines(str(result))此代碼輸出如下:8.8.8.8Sent = 4Received = 4Lost = 00% LossMaximum = 279msMinimum = 158msAverage = 195ms------------------------------------------------------------127.0.0.1Sent = 4Received = 4Lost = 00% LossMaximum = 0msMinimum = 0msAverage = 0ms------------------------------------------------------------但是,當(dāng)我檢查名為“outputp.txt”的輸出文件時(shí),我發(fā)現(xiàn)只有最后一個(gè)結(jié)果如下所示:Average = 0ms
查看完整描述

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))


查看完整回答
反對(duì) 回復(fù) 2023-06-20
  • 1 回答
  • 0 關(guān)注
  • 181 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)