Jamgreen
Jamgreen

Reputation: 11039

Save vectors to file in Python with NumPy

I have a variable with a numeric value, a variable with a string value, and two vectors defined with NumPy

a = 10
b = "text string"
positions = np.array([])
forces = np.array([])

I want to save these values to a file. I've used http://docs.scipy.org/doc/numpy/reference/generated/numpy.savetxt.html to save the two vectors with

np.savetxt('test.out', (positions,forces))

but I also need to store the values of a and b.

How is this possible?

Upvotes: 1

Views: 3339

Answers (1)

Wolph
Wolph

Reputation: 80031

Personally I would recommend using numpy.savez and numpy.load. For example:

numpy.savez('test.npz', a=a, b=b, positions=positions, forces=forces)

You can load it again like this:

data = numpy.load('test.npz')
a = data['a']

Upvotes: 1

Related Questions