0

This question is connected to others already existing (such as this), but I could not solve by following the solutions provided in there. So I try to ask again.

I have 7 parameters, say a1, a2, b1, b2, b3, b4 and b5. The 'a' parameters are integers, while the 'b' parameters are floats. As an example, take

a1=1, a2=500, b1=1.0, b2=1.0, b3=-0.866025, b4=0.0, b5=-0.1.

I would like to save these parameters into a file. The code for doing this reads:

f = open("params.txt",'w')
arr=np.array((a1,a2,b1,b2,b3,b4,b5))
arrform=' '.join(['%d']*2 + ['%f']*5)
np.savetxt(f,arr,fmt=arrform)
f.close()

When executing this code I get the following error message:

fmt has wrong number of % formats: %d %d %f %f %f %f %f

Could you, please, tell me what is my mistake?

1
  • 1
    print(arrform%(a1,a2,...), file=f) of f.write(...) should be enough if those variables are scalars. You don't need numpy to format and write a tuple of numbers. Commented Nov 30, 2020 at 15:47

1 Answer 1

1

use column_stack instead array

f = open("params.txt",'w')
arr=np.column_stack((a1,a2,b1,b2,b3,b4,b5))
arrform=' '.join(['%d']*2 + ['%f']*5)
np.savetxt(f,arr,fmt=arrform)
f.close()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.