如何从某一行开始将numpy数组写入.txt文件?numpy 1.6版

2024-09-28 22:04:44 发布

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

在:How to write numpy arrays to .txt file, starting at a certain line?

人们帮助我解决了我的问题-这适用于Numpy1.7或更高版本。不幸的是,我不得不使用1.6版的代码(谢谢@Praveen)

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=''

给我一个Numpy1.6的错误

^{pr2}$

对于版本1.6,是否有一个解决方案可以产生相同的结果:

# Filexy
# time operation1 operation2
Answer to life, the universe and everything = 42
0   12  100
60  23  123
120 68  203
180 26  301

Tags: andthetotextanswer版本txttime
1条回答
网友
1楼 · 发布于 2024-09-28 22:04:44

你先写你的头,然后你转储数据。 请注意,您将需要在标题的每一行中添加#,因为np.savetxt无法做到这一点。在

time = np.array([0,60,120,180])
operation1 = np.array([12,23,68,26])
operation2 = np.array([100,123,203,301])
header='#Filexy\n#time  operation1 operation2'
with open('example.txt', 'w') as f:
    f.write(header)
    np.savetxt(f, np.c_[time, operation1, operation2],
                   fmt='%d',
                   delimiter='\t')

相关问题 更多 >