Rhinocerotidae
Rhinocerotidae

Reputation: 925

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

Upvotes: 2

Views: 4544

Answers (3)

Razik
Razik

Reputation: 182

You forgot the '+' sign between str(k) and '.txt'

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

Upvotes: 1

BPL
BPL

Reputation: 9863

You forgot a + sign, try replacing with this:

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

Upvotes: 1

Harrison
Harrison

Reputation: 5386

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

You forgot a + sign.

Upvotes: 5

Related Questions