Search code examples
pythondataframenumpyjupytertxt

Need to have N txt files created, np.savetxt


for a math paper I need to make N slices and output them to as many different txt files as there are slices made. Everything should be saved in TXT. I mean, I need to write each new step of the cycle to a new file.

N=10
j=1
for i in range (1,N):
    df_step=df2 [j::N]
    j+=1
    np.savetxt(r'c:\Data\step.txt', df_step, fmt='%s')

need each step of the loop to be saved to a file named step_1, step_2, step_3, ...., step_i. np.savetxt(r'c:\Data\step.txt', df_step, fmt='%s') What should i change here?



Solution

  • If your df_step is what you want, you can use the .to_csv function call for dataframes:

    for i in range (1,N):
        df_step=df2[j::N]
        j+=1
        df_step.to_csv("Step_"+str(i)+".txt")
    

    You can specify formats and separators as well in the function