numpy 将数组写入. txt文件

dkqlctbz  于 2023-03-23  发布在  其他
关注(0)|答案(1)|浏览(125)

在Python中,我需要将一个numpy-arrays列表(见附件图片)写入一个txt.-file,然后在一个新的python-file中重用原始的数组列表。我如何才能做到这一点?

read_files = open("read_files.txt","w+")

content = str(df)

read_files.write(content)
read_files.close()

然后在新的python文件中,我这样做了:

file = open("read_files.txt", "r")
content = file.read()

但是,当我想在新的python-file中对数组列表进行切片时,我得到以下错误:

TypeError: string indices must be integers
hsgswve4

hsgswve41#

对3D阵列使用np.save

arr = np.random.random((20, 10, 5))

np.save('arr', arr)  # extension .npy

arr2 = np.load('arr.npy')

注意:该文件不是人类可读的。
输出:

>>> np.all(arr2 == arr)
True

相关问题