Can anyone guide me on how to add a header to multiple csv files?
Optional: If anyone knows a method add add a header to pre-existing files in C# or can guide me to the relevant resources. That would be great.
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.")
eval("fault_class")seems like a difficult way to writefault_classand makes the code harder to read.eval, and its cousinexec, will execute arbitrary code without checking if it's safe code. Most of the time, whenever you are using those functions (especially a lot), you can refactor to a more efficient and pythonic implementation