在数组中组合整数和字符串并保存到文本文件

2024-09-30 18:25:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我有几个numpy数组,只有一个包含整数。我想将它们组合成一个数组,并将其保存在一个.txt文件中。最后一行给我带来了麻烦,因为我试图将整数和字符串结合起来:

import numpy as np


specimen = np.array(['one1', 'two2', 'three3'])

outpath = '/some_folder/'

M1_x_list = np.array([1,2,3])
M1_y_list = np.array([2,3,4])
M1_z_list = np.array([4,5,6])


ALL_OUTPUT = np.asarray([specimen, M1_x_list, M1_y_list, M1_z_list]).T
print ALL_OUTPUT

np.savetxt(outpath+'test.txt', (ALL_OUTPUT), delimiter='\t', newline='\n', header='specimen \t x \t y \t z\t ', footer='')

我的预期产出是:

['one1' '1' '2' '4']
['two2' '2' '3' '5']
['three3' '3' '4' '6']

Tags: numpytxtoutputnp整数数组allarray