Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在python中将30维numpy数组保存为人类可读的txt文件?_Python_Arrays_Numpy_Save - Fatal编程技术网

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

如何在python中将30维numpy数组保存为人类可读的txt文件?,python,arrays,numpy,save,Python,Arrays,Numpy,Save,我是python新手,有一个问题。我有下面的数组,想把它保存到一个txt文件中 data_arr=np.array([[str(drahtnumer).zfill(4),str(Lagenummer).zfill(4),位置在{0:07.2f}.format(x_mid),“{0:07.2f}.format(y_mid),“{0:07.2f}.format(z_0),“{0:06.3f}.format(r)].format(r)] 打印(数据)看起来像: [['0001' '0001' '01.

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

data_arr=np.array([[str(drahtnumer).zfill(4),str(Lagenummer).zfill(4),位置在{0:07.2f}.format(x_mid),“{0:07.2f}.format(y_mid),“{0:07.2f}.format(z_0),“{0:06.3f}.format(r)].format(r)]

打印(数据)看起来像:

[['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函数中,以防有什么不同^^


非常感谢你的帮助

您可以尝试这种格式

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)

你需要显示你想要的文本文件的样子。你可能想使用我更新的帖子!不,我需要一个.txt文件-.-`def save_file(data_arr):a_file=open(“test.txt”,“w”)用于data_arr:np.savetxt(a_file,row)中的行a_file.close()`打开一个test.txt文件,但它是空的,并且程序仍然在没有完成的情况下退出(它没有save_files函数)@Sven Lankeshofer您在执行之前是否将test.txt文件设置为空,因为上面的线程仅供参考。创建之后,您必须运行代码。不,我没有。但现在有了这个文件,如果我运行代码,它仍然无法填充它,程序崩溃,但只有以下信息/没有信息说明原因:进程已完成,退出代码为-1073740791(0xC0000409)。--当我手动创建一个新的测试文件,然后启动程序时,它工作了!解决这个问题的另一种方法是在open中使用“w+”(“test.txt”,“w+”)
original_array = np.loadtxt("test.txt").reshape(dims)

print(original_array)