gpt4 book ai didi

python - numpy 将二维数组保存到文本文件

转载 作者:太空狗 更新时间:2023-10-29 21:31:43 25 4
gpt4 key购买 nike

我用

np.savetxt('file.txt', array, delimiter=',')

将数组保存到以逗号分隔的文件中。看起来像:

1, 2, 3
4, 5, 6
7, 8, 9

如何将数组保存到以 numpy 格式显示的文件中。换句话说,它看起来像:

[[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]

最佳答案

In [38]: x = np.arange(1,10).reshape(3,3)    

In [40]: print(np.array2string(x, separator=', '))
[[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]

将 NumPy 数组 x 保存到文件中:

np.set_printoptions(threshold=np.inf, linewidth=np.inf)  # turn off summarization, line-wrapping
with open(path, 'w') as f:
f.write(np.array2string(x, separator=', '))

关于python - numpy 将二维数组保存到文本文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22821460/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com