'Empty space in between rows after using writer in python
I am using csv package now, and every time when I write to a new csv file and open it with excel I will find a empty row in between every two rows.
filereader = csv.reader(open("tests.csv", "r"), delimiter=",")
filewriter = csv.writer(open("test_step1.csv", "w"), delimiter=",")
#Delete header
for row in filereader:
if row[0].isdigit():
filewriter.writerow(row)

Solution 1:[1]
I am using Python 3.6 and it seems that
csv.writer(open("test_step1.csv", "wb"), delimiter=",", newline="")
is not correct. Rather, newline="" should added in the open() clause.
Solution 2:[2]
Just add newline='' where you are opening the file. It works for me. e.g., csvFileName = open('file.csv', 'w', newline= '')
Solution 3:[3]
Please note that csv 1.0 in Windows (requires '\n') and MAC OS (optional). The code below will output .csv file with no spaces between next line. It works in MAC and Windows.
import csv
for i in range (5):
MyList=[]
MyList.append(i)
cnword=0
with open('test_step1.csv', 'a') as f:
for item in MyList:
if cnword != len(MyList)-1:
f.write("%s," % item)
else:
f.write("%s" % item)
cnword+=1
f.write("\n" )
myList=[]
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Krushnakant Ladani |
| Solution 2 | Seyyed Hossein SeyyedAghaei Re |
| Solution 3 | Trees |
