为什么当我使用 Python 将列表转换为 CSV 文件时,我的数据之间有空行?

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

希望大家一切都好

我有一个问题,所以我有一个数据列表,我想将其转换为 CSV 文件。当我打印数据列表时,它没有任何问题。但是后来我打开CSV文件,数据之间有空行。

CSV File with Blank Rows Between Datas

这是代码

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

同样,列表是正确的,没有空行。问题是 CSV 文件。

在此先感谢您!

您需要在打开文件时添加 newline='' 以去除空行

import csv

with open('file_name.csv', 'w' , newline='') as dbcsvfile:
    writer = csv.writer(dbcsvfile)
    writer.writerows(db)