简体   繁体   中英

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

before writing this post, I tried to do it by myself but give up.

I read this one and this but I could not get it

This is the code to show us latency data:

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

The output is:

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

I would like to only save these data like this figure在此处输入图像描述

Use:

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

print (x)

to assign to a variable.

EDIT:

This is the complete code:

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM