python-将多个标头写入csv文件

时间:2020-11-04 11:17:48

标签: python loops csv header

我非常感谢帮助我通过仅编写一次标头来解决此错误

try:
    with open(save_folder + 'all.csv', 'a', newline='') as csvfile_new:
        spamwriter = csv.writer(csvfile_new, delimiter=',')
        for i in range(np.shape(csv_data)[0]): #Count through all rows in list and save to csv
            spamwriter.writerow(csv_data[i][:])

    print("Processing of file named {} is completed.".format(file))
except PermissionError:
    print("\n--- PERMISION ERROR ---")
    print("CSV FILE NAMED: {} COULD NOT BE PROCESSED".format(patient_id + '.csv'))
    print("PLEASE ENSURE THIS SPREADSHEET IS NOT BEING USED BY ANOTHER PROGRAM\n")

这就是我的文件的样子 csv file

1 个答案:

答案 0 :(得分:0)

    with open(save_folder + 'all.csv', 'a', newline='') as csvfile_new:

    spamwriter = csv.writer(csvfile_new, delimiter=',')
    count = 0
    header_row =[]
    for i in csv_data: #Count through all rows in list and save to csv
        other_rows = []
        if count == 0:
            header_row.append(i)
            spamwriter.writerow(header_row)
            count = count+1
        other_rows.append(i)
        spamwriter.writerow(other_rows)
相关问题