How to add a empty row on excel file from python

Viewed 69

I have been using this code to extract data from website and store it into a csv file. However, I would like to know how can I add an empty row between the data without using hardcode, for instance a empty row that seperate the title 'Total' and the data extracted from Total.text. (Like this picture.) This is the link I extra data "https://finance.yahoo.com/quote/0700.HK/sustainability?p=0700.HK" like this

with open(f'{stockname}_sustainability.csv', 'w', newline='') as csvfile:
    fieldnames = ['Total','Type','Percentile','ERS','SRS','GRS','mclevel']
    writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
    writer.writeheader()
    writer.writerow({'Total':int(Total.text),'Type' : Type.text,'Percentile':int(Percentile[0:2]), 'ERS' : float(ERS.text),'SRS':float(SRS.text),'GRS':float(GRS.text),'mclevel':int(mclevel.text)})
1 Answers
import csv

with open('Book1.csv', 'w', newline='') as csvfile:
    writer = csv.writer(csvfile, delimiter=',')
    fieldnames = ['Total', 'Type', 'Percentile', 'ERS', 'SRS', 'GRS', 'mclevel']
    writer.writerow("".join(fieldnames))
    writer.writerow("\n")
    writer.writerow({'Total':int(Total.text),'Type' : Type.text,'Percentile':int(Percentile[0:2]), 'ERS' : float(ERS.text),'SRS':float(SRS.text),'GRS':float(GRS.text),'mclevel':int(mclevel.text)})

By using write.writerow("\n") you will create one empty row.

Related