Saravana Kumar Saravana Kumar - 3 months ago 35
Python Question

np.savetxt - using variable for file name

How can I use variable for a file name when using

np.savetxt


Here is my MWE:

import numpy as np

k=0
x = [1, 2, 3]
y = [4, 5, 6]
zipped = zip(x, y)

np.savetxt('koo'+str(k)'.txt', zipped, fmt='%f\t%f')


However this throws me 'invalid syntax' error

Answer
np.savetxt('koo'+ str(k) + '.txt', zipped, fmt='%f\t%f')

You forgot a + sign.