I'm trying to create a csv file with ping times. Below I have the code to get the ping time and put it in the csv file. Unfortunately the format in the csv files is a little messed up.
import subprocess as sp
import csv
ip = "216.52.241.254"
status,result = sp.getstatusoutput("ping -n 1 -w 1000 " + ip + ' | grep -o time=[0-9]* | grep -o [0-9]*')
with open('C:/PingData/test.csv', 'a') as csvfile:
#result = int(result)
cwriter = csv.writer(csvfile, delimiter=' ', quotechar='|', quoting=csv.QUOTE_MINIMAL)
cwriter.writerow(result)
The csv file that this code is printing looks like:
What it should look like is this:
I'm looking for a solution/reason why there are extra blank rows being printed, and why there are spaces between the numbers (I've tried passing them in as integers to no avail)
writerow
expects a list, so it takes your string as a list of characters. Put it in brackets to treat it as a list of a single string:
cwriter.writerow([result])
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments