我需要将数据保存到一个文件中,其中每行都遵循以下格式:<string1> <array of thousands of floats> <string2>
。所以我考虑将数据连接成一个巨大的字符串数组,如下所示:
labels = ['label1', 'label2', 'label3']
values = [[0.1, 0.4, 0.5],
[0.1, 0.2, 0.1],
[0.5, 0.6, 1.0]]
descriptions = ['desc1', 'desc2', 'desc3']
concat1 = np.r_['1,2,0', labels, values]
concat2 = np.r_['1,2,0', concat1, descriptions]
结果:
[['label1' '0.1' '0.4' '0.5' 'desc1']
['label2' '0.1' '0.2' '0.1' 'desc2']
['label3' '0.5' '0.6' '1.0' 'desc3']]
我知道,如果每个子阵列足够小,我可以这样做:
np.savetxt('output.txt', concat2, fmt = "%s %s %s %s %s")
但我的问题涉及数千个值,所以一次键入一个变量的格式是不切实际的。你知道吗
关于如何将其保存到文件中还有其他建议吗?你知道吗
PS:把浮点数保存为字符串听起来有点奇怪,但我的上级这样问,所以。。。你知道吗
没有
numpy
的解决方案:output.txt
的内容:或者从
concat2
开始:相关问题 更多 >
编程相关推荐