将标题添加到多个csv文件

时间:2020-04-17 20:02:10

标签: python csv

有人可以指导我如何将标题添加到多个csv文件吗?

可选:如果有人知道方法,请在C#中为现有文件添加头,或者可以将我引向相关资源。那就太好了。

import os
import os.path as path

## First create a function that will generate random files.
def create_random_csv_files(fault_classes, number_of_files_in_each_class):
    os.mkdir("./random_data/")  # Make a directory to save created files.
    for fault_class in fault_classes:
        for i in range(number_of_files_in_each_class):
            data = np.random.rand(1024,3)
            file_name = "./random_data/" + eval("fault_class") + "_" + "{0:03}".format(i+1) + ".csv" # This creates file_name
            np.savetxt(eval("file_name"), data, delimiter = ",", comments = "")
        print(str(eval("number_of_files_in_each_class")) + " " + eval("fault_class") + " files"  + " created.")

1 个答案:

答案 0 :(得分:0)

import os
import os.path as path

## First create a function that will generate random files.
def create_random_csv_files(fault_classes, number_of_files_in_each_class):
    os.mkdir("./random_data/")  # Make a directory to save created files.
    for fault_class in fault_classes:
        for i in range(number_of_files_in_each_class):
            data = np.random.rand(1024,3)
            file_name = "./random_data/" + eval("fault_class") + "_" + "{0:03}".format(i+1) + ".csv" # This creates file_name
            np.savetxt("file_name", data, delimiter = ",", header = "V1,V2,V3", comments = "")
        print(str("number_of_files_in_each_class") + " " + "fault_class" + " files"  + " created.")