我不确定你尝试了什么,但你需要使用header
参数输入np.savetxt
。此外,您需要正确连接数组。最简单的方法是使用np.c_ http://docs.scipy.org/doc/numpy/reference/generated/numpy.c_.html,这会将您的一维数组强制转换为二维数组,然后按照您期望的方式连接它们。
>>> time = np.array([0,60,120,180])
>>> operation1 = np.array([12,23,68,26])
>>> operation2 = np.array([100,123,203,301])
>>> np.savetxt('example.txt', np.c_[time, operation1, operation2],
header='Filexy\ntime operation1 operation2', fmt='%d',
delimiter='\t')
example.txt
现在包含:
# Filexy
# time operation1 operation2
0 12 100
60 23 123
120 68 203
180 26 301
还要注意使用fmt='%d'
获取输出中的整数值。savetxt
默认情况下将保存为浮点数,即使对于整数数组也是如此。
关于分隔符,您只需要使用delimiter
争论。这里还不清楚,但事实上,列之间有选项卡。例如,vim
显示使用点的选项卡:
# Filexy
# time operation1 operation2
0· 12· 100
60· 23· 123
120·68· 203
180·26· 301
附录:
如果你想添加标题and在数组之前添加额外的行,您最好创建一个自定义标头,并包含您自己的注释字符。使用comment
阻止的论点savetxt
从添加额外的#
's.
>>> extra_text = 'Answer to life, the universe and everything = 42'
>>> header = '# Filexy\n# time operation1 operation2\n' + extra_text
>>> np.savetxt('example.txt', np.c_[time, operation1, operation2],
header=header, fmt='%d', delimiter='\t', comments='')
产生
# Filexy
# time operation1 operation2
Answer to life, the universe and everything = 42
0 12 100
60 23 123
120 68 203
180 26 301