如何将 30 维 numpy 数组保存到 python 中人类可读的 txt 文件?

How to save a 30- dimensional numpy array to human readable txt file in python?

我是 python 的新手,有一个问题。我有以下数组,想将其保存到 txt 文件中。

data_arr = np.array([[str(Drahtnummer).zfill(4), str(Lagenummer).zfill(4), Position_in_Lage, "{0:07.2f}".format(x_mid), "{0:07.2f}".format(y_mid), "{0:07.2f}".format(z_0),"{0:06.3f}".format(r_g), "{0:06.3f}".format(r)]])

print(data_arr) 看起来像:

[['0001' '0001' '01.01' '0002.32' '0002.00' '0001.00' '02.000' '01.000']
 ['0002' '0001' '02.01' '0002.92' '0005.95' '0001.00' '02.000' '01.000']
 ['0003' '0001' '03.01' '0003.52' '0009.91' '0001.00' '02.000' '01.000']
 ['0004' '0001' '04.01' '0004.12' '0013.86' '0001.00' '02.000' '01.000']
 ['0005' '0001' '05.01' '0004.72' '0017.82' '0001.00' '02.000' '01.000']
 ['0006' '0002' '01.02' '0006.05' '0003.46' '0001.00' '02.000' '01.000']
 ['0007' '0002' '02.02' '0006.65' '0007.41' '0001.00' '02.000' '01.000']
 ['0008' '0002' '03.02' '0007.25' '0011.37' '0001.00' '02.000' '01.000']
 ['0009' '0002' '04.02' '0007.85' '0015.32' '0001.00' '02.000' '01.000']
 ['0010' '0003' '01.03' '0009.77' '0004.92' '0001.00' '02.000' '01.000']
 ['0011' '0003' '02.03' '0010.37' '0008.87' '0001.00' '02.000' '01.000']
 ['0012' '0003' '03.03' '0010.97' '0012.83' '0001.00' '02.000' '01.000']
 ['0013' '0003' '04.03' '0011.57' '0016.78' '0001.00' '02.000' '01.000']
 ['0014' '0004' '01.04' '0012.90' '0002.42' '0001.00' '02.000' '01.000']
 ['0015' '0004' '02.04' '0013.50' '0006.37' '0001.00' '02.000' '01.000']
 ['0016' '0004' '03.04' '0014.10' '0010.33' '0001.00' '02.000' '01.000']
 ['0017' '0004' '04.04' '0014.70' '0014.28' '0001.00' '02.000' '01.000']
 ['0018' '0005' '01.05' '0016.62' '0003.88' '0001.00' '02.000' '01.000']
 ['0019' '0005' '02.05' '0017.22' '0007.83' '0001.00' '02.000' '01.000']
 ['0020' '0005' '03.05' '0017.82' '0011.79' '0001.00' '02.000' '01.000']
 ['0021' '0005' '04.05' '0018.42' '0015.74' '0001.00' '02.000' '01.000']
 ['0022' '0006' '01.06' '0020.35' '0005.33' '0001.00' '02.000' '01.000']
 ['0023' '0006' '02.06' '0020.95' '0009.29' '0001.00' '02.000' '01.000']
 ['0024' '0006' '03.06' '0021.55' '0013.24' '0001.00' '02.000' '01.000']
 ['0025' '0006' '04.06' '0022.15' '0017.20' '0001.00' '02.000' '01.000']
 ['0026' '0007' '01.07' '0023.47' '0002.84' '0001.00' '02.000' '01.000']
 ['0027' '0007' '02.07' '0024.07' '0006.79' '0001.00' '02.000' '01.000']
 ['0028' '0007' '03.07' '0024.67' '0010.75' '0001.00' '02.000' '01.000']
 ['0029' '0007' '04.07' '0025.27' '0014.70' '0001.00' '02.000' '01.000']
 ['0030' '0008' '01.08' '0027.20' '0004.29' '0001.00' '02.000' '01.000']]

这种格式没有边缘,用“;”分隔会很完美。

我是对的,我有一个二维数组,每行 30 行,每行 8 列。

如何将其转换成可读的txt文件?我尝试了经常引用的“将其切成薄片”,但我真的不知道如何重塑它。我试着把它放到一个自己的 save_file 函数中以防有任何差异^^

喜欢

def save_file(data_arr):
    np.savetxt("OWA.txt", data_arr)

非常感谢您的帮助!

你可以试试这个格式。

a_file = open("test.txt", "w")
for row in an_array:
    np.savetxt(a_file, row)

a_file.close()

以及当您打开文件时。你可以试试这个

original_array = np.loadtxt("test.txt").reshape(dims)

print(original_array)