简体   繁体   English

使用NumPy将向量保存到Python文件中

[英]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 我有一个带数字值的变量,一个带字符串值的变量以及两个用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 我已经使用http://docs.scipy.org/doc/numpy/reference/generation/numpy.savetxt.html来保存两个向量

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

but I also need to store the values of a and b . 但是我还需要存储ab的值。

How is this possible? 这怎么可能?

Personally I would recommend using numpy.savez and numpy.load . 我个人建议使用numpy.saveznumpy.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']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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