【问题标题】:How to add a empty row on excel file from python如何从python在excel文件上添加一个空行
【发布时间】:2021-04-16 13:17:21
【问题描述】:

我一直在使用此代码从网站中提取数据并将其存储到 csv 文件中。但是,我想知道如何在不使用硬代码的情况下在数据之间添加一个空行,例如分隔标题“Total”和从 Total.text 中提取的数据的空行。 (像这张照片。)这是我额外数据的链接“https://finance.yahoo.com/quote/0700.HK/sustainability?p=0700.HK”

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

【问题讨论】:

    标签: python-3.x excel csv


    【解决方案1】:
    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)})
    

    通过使用 write.writerow("\n") 您将创建一个空行。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-02
      • 1970-01-01
      • 2021-03-30
      • 2017-04-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多