简体   繁体   中英

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?

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']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM