Python Forum

Full Version: numpy.savetxt()
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,

this code works fine:

import numpy as np
  
data = np.array([3,2,55,4])

for i in range(4):
    data[i] = data[i]+3

    print("text before", data[i], "text after")
Output:
Output:
~/pro$ python max.py ('text before', 6, 'text after') ('text before', 5, 'text after') ('text before', 58, 'text after') ('text before', 7, 'text after')
But I can't save in the file output.txt.
I tried with numpy.savetxt()

np.savetxt("output.txt", ???, fmt='%i', delimiter=",")
What should i add in this code?

Thanks

m.
Can do it like this,since you take values out of the numpy arrary.
import numpy as np

data = np.array([3,2,55,4])
with open('output.txt', 'w') as f_out:
    for i in range(4):
        data[i] = data[i]+3
        print(f"text before, {data[i]}, text after")
        f_out.write(f"text before, {data[i]}, text after\n")
Output:
text before, 6, text after text before, 5, text after text before, 58, text after text before, 7, text after
(Jun-26-2021, 07:16 AM)snippsat Wrote: [ -> ]Can do it like this,since you take values out of the numpy arrary.
import numpy as np

data = np.array([3,2,55,4])
with open('output.txt', 'w') as f_out:
    for i in range(4):
        data[i] = data[i]+3
        print(f"text before, {data[i]}, text after")
        f_out.write(f"text before, {data[i]}, text after\n")
Output:
text before, 6, text after text before, 5, text after text before, 58, text after text before, 7, text after

Now it works fine.

Thank you,

m.