使用python编写csv文件时如何添加标题

时间:2019-07-18 12:30:22

标签: python selenium csv header

我检索了几个页面的html标签,并且要将它们保存在标题为以下内容的csv文件中: a,b,c d,requete,url_resultat,url范围 但是我找不到你能帮助我的东西。

with open('requetetest123.csv', 'w',newline='') as csvFile:
        writer = csv.DictWriter(csvFile, fieldnames = ["a" , "b" , "c" , "d" , "requete" , "url_resultat" , "url_rang"], delimiter = ';')
        writer.writeheader()
        for row in csvReader:
            requete=row[0]
            h=get_result_line(send_requete(browser,requete))
            row_write= (requete,h)
            writer.writerow(row_write)
    csvFile.close()

1 个答案:

答案 0 :(得分:0)

要在csv中添加标题,请尝试以下代码。

import csv
fieldnames = ["a", "b", "c", "d", "requete", "url_resultat", "url_rang"]
with open('requetetest123.csv', 'w', newline='') as csvFile:
    writer = csv.writer(csvFile, delimiter=';')
    writer.writerow(h for h in fieldnames)