Reputation: 925
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
Upvotes: 2
Views: 4544
Reputation: 182
You forgot the '+' sign between str(k) and '.txt'
np.savetxt('koo'+ str(k) + '.txt', zipped, fmt='%f\t%f')
Upvotes: 1
Reputation: 9863
You forgot a +
sign, try replacing with this:
np.savetxt('koo'+str(k)+'.txt', zipped, fmt='%f\t%f')
Upvotes: 1
Reputation: 5386
np.savetxt('koo'+ str(k) + '.txt', zipped, fmt='%f\t%f')
You forgot a +
sign.
Upvotes: 5