0

Maybe this is a simple question but I can't find in the community

I tried to write in files

import csv
f_out = open('filename.csv','w')
csvwriter = csv.writer(f_out)
li = [1,2,3,4,5,6]
csvwriter.writerow(li)
csvwriter.writerow(li)
csvwriter.writerow(li)
csvwriter.writerow(li)
f_out.close()

And instead of this :

1,2,3,4,5,6
1,2,3,4,5,6
1,2,3,4,5,6
1,2,3,4,5,6

I get this :

1,2,3,4,5,6

1,2,3,4,5,6

1,2,3,4,5,6

1,2,3,4,5,6

And 2 another empty line at the end of the file.

anyone can say where is my problem?

Ebrahim Bashirpour
  • 717
  • 2
  • 10
  • 21

1 Answers1

2

The solution is quite easy. In the open method you need to add a parameter newline=''.

import csv
f_out = open('filename.csv','w',newline='')
csvwriter = csv.writer(f_out)
li = [1,2,3,4,5,6]
csvwriter.writerow(li)
csvwriter.writerow(li)
csvwriter.writerow(li)
csvwriter.writerow(li)
f_out.close()

I think this should help you... For further details you can refer CSV Writer Documentation link

Anurag Singh
  • 126
  • 5
  • oh. Thank you. It works. sorry but I have another little question. in the end of this file, I get one empty line. what can I do to don't have an empty line in the end of the file? – Ebrahim Bashirpour Aug 27 '20 at 09:36