我正在将矩阵写入文本文件,需要在另一个python脚本中读取该文件。第二个脚本需要将文本恢复为numpy数组。我一直在努力寻找如何做到这一点,任何帮助将不胜感激。下面给出了两个数组示例:
[[ 0. 0. -0.]
[ 0. 0. 0.]
[ 0. 0. -0.]
[ 0. 0. 0.]
[ 0. 0. 0.]
[ 0. 0. 0.]
[ 0. 0. 0.]
[ 0. 0. 0.]
[ 0. 0. 0.]
[ 0. 0. 0.]
[ 0. 0. 0.]]
或
[[ 0. 0. -0.03011621]
[ 0. 0. 0. ]
[ 0. 0. 0. ]
[ 0. 0. 0. ]
[ 0. 0. 0. ]
[ 0. 0. 0. ]
[ 0. 0. 0. ]
[ 0. 0. 0. ]
[ 0. 0. 0. ]
[ 0. 0. -0.06023241]
[ 0. 0. -0.01204648]]
答案 0 :(得分:2)
要保存到文本文件,请使用np.savetxt
In [115]: x = np.zeros((10, 3))
In [116]: np.savetxt('/tmp/test.out', x)
要加载,请使用np.loadtxt或np.genfromtxt:
In [117]: y = np.genfromtxt('/tmp/test.out')
In [120]: y.shape
Out[120]: (10, 3)