簡體   English   中英

如何將os.system的output保存到.csv文件?

[英]How to save the output of os.system to .csv file?

在寫這篇文章之前,我試圖自己做,但放棄了。

我讀過這個這個,但我無法得到它

這是向我們顯示延遲數據的代碼:

import os
x = os.system("ping 192.168.1.1")

output 是:

PING 192.168.1.1 (192.168.1.1) 56(84) bytes of data.
64 bytes from 192.168.1.1: icmp_seq=1 ttl=64 time=2.47 ms
64 bytes from 192.168.1.1: icmp_seq=2 ttl=64 time=2.97 ms
64 bytes from 192.168.1.1: icmp_seq=3 ttl=64 time=3.02 ms
64 bytes from 192.168.1.1: icmp_seq=4 ttl=64 time=2.74 ms
64 bytes from 192.168.1.1: icmp_seq=5 ttl=64 time=2.74 ms
64 bytes from 192.168.1.1: icmp_seq=6 ttl=64 time=2.08 ms

--- 192.168.1.1 ping statistics ---
6 packets transmitted, 6 received, 0% packet loss, time 5007ms
rtt min/avg/max/mdev = 2.087/2.674/3.020/0.319 ms

我只想像這個圖一樣保存這些數據在此處輸入圖像描述

利用:

import os
x = os.popen('ping 198.168.1.1').read()

print (x)

分配給一個變量。

編輯:

這是完整的代碼:

import os

ip = "192.168.1.1"

x = os.popen("ping "+ip).read()

print (x)

x = x.splitlines()

x = x[len(x)-1]


x = x.replace("rtt min/avg/max/mdev = ","")

x = x.replace(" ms" , "")
x = x.replace("/" , ",")

x = ip +","+ x

file = open("newfile.csv","w")
file.write(x)
print(x)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM