nekovolta
nekovolta

Reputation: 516

Remove a blank line when I use writerow with CSV files

I am writing a CSV file, using the next code:

import csv

with open("doubt.csv", "w") as outfile:
    csvfile = csv.writer(outfile)
    csvfile.writerow(['A', 'B', 'C', 'D', 'E']) 
    csvfile.writerow(['A', 'B', 'C', 'D', 'E'])
    csvfile.writerow(['A', 'B', 'C', 'D', 'E'])

The problem is that each time write a row, it puts a "blank line" in the middle of the data:

-- -- -- -- --
A B C D E
A B C D E
A B C D E

How can I remove that blank line?

Upvotes: 2

Views: 1118

Answers (1)

IoaTzimas
IoaTzimas

Reputation: 10624

You need to add newline='' to avoid extra (empty) lines:

import csv

with open("doubt.csv", "w", newline='') as outfile:
    csvfile = csv.writer(outfile)
    csvfile.writerow(['A', 'B', 'C', 'D', 'E']) 
    csvfile.writerow(['A', 'B', 'C', 'D', 'E'])
    csvfile.writerow(['A', 'B', 'C', 'D', 'E'])

Upvotes: 1

Related Questions