Search code examples
pythoncsvexport-to-csv

Why there are blank rows between my data when I converted list to CSV file using Python?


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!


Solution

  • 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)