在 np.savetxt python 中保存带有标题和行名称的列



我有一个数组:

array([[  1.,    5.,    9.,   13.,    17.,    21.,    25.],
   [ 2.,    6.,    10.,    14.,    18.,    22.,    26.],
   [  3.,    7.,    11.,   15.,    19.,    23.,    27.],
   [ 4.,    8.,    12.,   16.,    20.,    24.,    28.]])

我想输入标题和行名称并保存在.txt扩展名中,如下所示:'

`      Column1 Column2 Column3 Column4 Column5 Column6 Column7                                                               
 Line1 1       5       9       13      17      21      25
 Line2 2       6       10      14      18      22      26
 Line3 3       7       11      15      19      23      27
 Line4 4       8       12      16      20      24      28

问题是当我保存时:

np.savetxt('data.txt', data, delimiter='t',fmt='%.18g') output: 1 5 9 13 17 21 25 6 10 14 18 22 26 7 11 15 19 23 27 8 12 16 20 24 28

Loop at numpy.savetxt((

您可以使用 np.savetxt('test.out', x, delimiter='t') 保存文件,其中 x 是数组。

但是,如果您正在使用表格(看起来您正在这样做(,您应该考虑在python中使用Pandas。Bu 使用 pandas 您可以将列名应用于表,并索引("行名",然后轻松地将表保存到文本文件中。

最新更新