如何从某行开始将numpy数组写入.txt文件? numpy版本1.6 [英] How to write numpy arrays to .txt file, starting at a certain line? numpy version 1.6

查看:128
本文介绍了如何从某行开始将numpy数组写入.txt文件? numpy版本1.6的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在:如何从某行开始将numpy数组写入.txt文件?

人们帮助我解决了问题-适用于numpy 1.7或更高版本.不幸的是,我必须使用1.6版-以下代码(感谢@Praveen)

People helped me to solve my problem - this works for numpy Version 1.7 or later. Unfortunatelly I have to use the version 1.6 - the follwoing code (thank you @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=''

给我numpy 1.6错误

give me an error with numpy 1.6

numpy.savetxt() got an unexpected keyword argument 'header' · Issue ...

版本1.6是否有产生相同结果的解决方法:

Is there a work-around for Version 1.6 that produces the same result:

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

推荐答案

首先编写标题,然后转储数据. 请注意,您需要在标题的每一行中添加#,因为np.savetxt不会这样做.

You write your header first, then you dump the data. Note that you'll need to add the # in each line of the header as np.savetxt won't do it.

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

这篇关于如何从某行开始将numpy数组写入.txt文件? numpy版本1.6的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆