Empty space in between rows after using writer in python
Asked Answered
M

5

11

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)

enter image description here

Marcum answered 31/5, 2013 at 19:40 Comment(3)
open the writer file in wb mode. It is because of the \r\nIndult
I tried, but got another error: "'str' does not support the buffer interface"Marcum
open both in binary mode. rb and wb You can read a string, and write binary the same datasetIndult
S
10

You need to open the file in wb mode try:

import csv

filereader = csv.reader(open("tests.csv", "r"), delimiter=",")
filewriter = csv.writer(open("test_step1.csv", "wb"), delimiter=",", newline="")
#Delete header
for row in filereader:
    if row[0].isdigit():
        filewriter.writerow(row)

The csv.writer writes \r\n into the file directly.

If you don't open the file in binary mode, it will write \r\r\n because on Windows text mode will translate each \n into \r\n.

edit:

For python 3 had to add newline="" to csv.writer as per this answer

Sacristy answered 31/5, 2013 at 19:46 Comment(4)
It gives me the error "'str' does not support the buffer interface" on the last line of code.Marcum
what version of python? Are you on windows?Sacristy
I got it! Thanks for your help though. You have to write open("", "w", newline='') instead! https://mcmap.net/q/82764/-python3-writing-csv-filesMarcum
That's great I went to the pub I'll update my answer so you can accept if if you wantSacristy
H
2

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.

Hodeida answered 29/11, 2017 at 4:42 Comment(0)
C
1

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=[]
Coffee answered 18/7, 2019 at 15:13 Comment(0)
S
1

Just add newline='' where you are opening the file. It works for me. e.g., csvFileName = open('file.csv', 'w', newline= '')

Superannuated answered 31/1, 2022 at 11:16 Comment(1)
Better to use binary mode. But this does work.Thatch
T
0

For those that using pandas.DataFrame, use lineterminator to remove space between row and '.write('\n') to add space between DataFrame.

import pandas as pd
import numpy as np


with open('report_.csv', 'w') as f:
    for _ in range(4):  # loop here
        df = pd.DataFrame(np.random.random((3,4)))

        df.to_csv(f,
                  index=True,  # to include index
                  header=True,  # to include header
                  lineterminator='\n',  # to remove whitespace between row
                 )
        f.write('\n')  # remove this to delete whitespace between DataFrame

From my original answer here

Tonguetied answered 23/11, 2023 at 10:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.