1

I hope everyone is doing fine.

I have a question, so I have a list of data that I want to convert it to CSV file. When I print the list of data, there's nothing wrong with it. But then I open the CSV file, there are blank rows between the data.

CSV File with Blank Rows Between Datas

And here is the code

with open(f'Scrapped Data\\covid_{int(day)}_{month}_{year}.csv', 'w') as dbcsvfile:
writer = csv.writer(dbcsvfile)
writer.writerows(db)

Again, the list is correct, no empty rows. The problem is the CSV file.

Thank you in advanced!

  • Please give us a *complete* example. We should be able to run the code without modification and without needing to create our own input, and observe the problem for ourselves. This means including the code that creates `db`, as well as all necessary `import` statements. – Karl Knechtel Apr 11 '21 at 09:09
  • https://stackoverflow.com/questions/40476677/specify-newline-character-n-in-reading-csv-using-python – George Ogden Mar 07 '23 at 11:14

1 Answers1

1

you need to add newline='' while opening the file in order to get rid of the blank lines

import csv

with open('file_name.csv', 'w' , newline='') as dbcsvfile:
    writer = csv.writer(dbcsvfile)
    writer.writerows(db)
Ahmed
  • 43
  • 1
  • 4