0

I have this code for writing a list to a CSV file, but when I run it, it creates spaces between each of the lines I want to write. To show you, this is my code:

savelst = [[1,2,3,4,5],[1,2,3,4,5]]
with open('file.csv', 'w') as f:
    writer = csv.writer(f, delimiter=',')
    writer.writerows(savelst)

and it gives me this result:

1,2,3,4,5

1,2,3,4,5

So I don't want the space in between.

3
  • Use the binary mode wb Commented Aug 2, 2018 at 12:19
  • Strange. I just ran your code using Python 3.6.1 and I did not get any whitespace between the lines. Which version did you use? Commented Aug 2, 2018 at 12:21
  • I used python 3.6 as well. But I already solved the problem. Commented Aug 2, 2018 at 17:02

1 Answer 1

3

Change the line where you open the file as:

with open('file.csv', 'w', newline='') as f:

Adding the newline argument as '' will remove the new lines from your output file.

Sign up to request clarification or add additional context in comments.

2 Comments

In fact it removes the additional newline.
I meant new lines, but I wrote spaces there by mistake. Corrected it now.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.