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.

1 Answers1

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.

Shashank Verma
  • 369
  • 1
  • 14