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

2024-02-22

At: 如何将 numpy 数组从某一行开始写入 .txt 文件? https://stackoverflow.com/questions/39483774/how-to-write-numpy-arrays-to-txt-file-starting-at-a-certain-line

人们帮助我解决了我的问题 - 这适用于 numpy 版本 1.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=''

给我 numpy 1.6 的错误

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

版本 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

首先编写标头,然后转储数据。 请注意,您需要添加#在标题的每一行中作为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')
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

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

随机推荐