Reputation: 516
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
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