0

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?


1
  • Use np.savetxt(fr'c:\Data\step_{i}.txt', df_step, fmt='%s'). And you can replace j by i, it's useless to have two counters. Commented Dec 18, 2023 at 10:13

1 Answer 1

0

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

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.