Python CSV, blank rows

Alter

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: enter image description here

What it should look like is this:
enter image description here

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)

Brent Washburne

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.

edited at
0

Comments

0 comments
Login to comment

Related

Writing to CSV with Python adds blank lines

Writing to CSV with Python adds blank lines

Remove blank rows from CSV file

Python Pandas - merge rows if some values are blank

Pandas: read_csv ignore rows after a blank line

Python CSV writer blank line

exporting an array inside a hashtable to CSV without blank rows

download csv but rows with comma is blank

Remove leading blank space from CSV results in insertion of blank rows and deletion of rows

remove blank rows in the CSV file using NiFi

is it possible to export an R dataframe to CSV with blank rows between groups?

Checking if the first cell of a CSV is blank in Python

Beeline-Hive returns CSV with blank rows on top of data

How can I delete/strip the white/blank rows in my csv file using Python?

How to get the updated csv file after deleting blank rows?

Removing "blank" or bad rows in a csv file using Perl.

Remove Blank ROWS from CSV files in php

Remove Blank Rows in CSV Using Python

Removing csv rows in Python

Insert blank rows every fifth row in csv file python

Python determine a blank column in a csv

RunSpacePool output CSV contains blank rows

csv writer is writing blank lines between rows (python 2.7)

How to add leading blank rows to a CSV?

Is there any way to count the no. of blank rows in csv file?

Missing out rows with blank spaces when writing to a new CSV file

Why there are blank rows between my data when I converted list to CSV file using Python?

Python. Code leaves blank rows in csv file after every row

Merging rows in python csv