如何以文本文件的形式保存一个二维数组,然后使用python从文本文件中读取它?

How to save a 2 dimensinal array in the form of text file and then read it from the text file using python?

我有一个二维数组。我想将这个数组保存在一个文本文件中,然后我想使用 python.

检索这个数组

我的代码:

np.savetxt('points.txt',final_points)
f=open('points.txt','r')
new_final_points=f.read()

这会将整个数组保存为 new_final_points 中的字符串。 我希望它以数组形式返回。

您可以使用与 np.savetxt

非常相似的方式使用 numpy 的 np.loadtxt

示例:

np.savetxt("points.txt")
array = np.loadtxt("points.txt")