在一个文件中存储多个二维坐标数组

问题描述 投票:0回答:1

我有一些坐标点数组,我想将它们存储在一个文件中,但我想对它们进行分隔。

我做什么:

array1 = np.random.rand(2, 2)
array2 = np.random.rand(2, 2)
np.savetxt('test1.txt', array1)
np.savetxt('test2.txt', array2)

test1.txt 示例

8.178330983529058518e-01 7.846138841153188492e-01
7.411074030801956258e-01 6.164560724010209602e-01

如何将它们存储在带有分隔符的同一文件中?我不是专门寻找 .txt 文件,而是我以后可以读取的文件。

我想要这样的东西:

8.178330983529058518e-01 7.846138841153188492e-01
7.411074030801956258e-01 6.164560724010209602e-01
%%%%%
6.732533221094740838e-01 7.317714192847557531e-02
9.745692442064657346e-01 2.045576523239983624e-01
python arrays numpy
1个回答
0
投票

将 numpy 导入为 np

以 open('test1.txt', 'a') 作为文件:

for k in range(1, 30):

    array1 = np.random.rand(2, 2)
    np.savetxt(file, array1, delimiter=' ', newline='\n', fmt='%1.18e', header='', footer='', comments='')
    file.write('%%%%%\n')
© www.soinside.com 2019 - 2024. All rights reserved.