【问题标题】:How to write numpy arrays to .txt file, starting at a certain line? numpy version 1.6如何从某一行开始将 numpy 数组写入 .txt 文件? numpy 1.6 版
【发布时间】:2016-09-14 09:58:38
【问题描述】:

在: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

【问题讨论】:

  • 为什么要使用已弃用的 numpy?如果是关于管理员权限,请查看 pyenv。

标签: python numpy


【解决方案1】:

您先编写标题,然后转储数据。 请注意,您需要在标题的每一行中添加#,因为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')

【讨论】:

    猜你喜欢
    • 2017-01-21
    • 2018-08-11
    • 2014-08-30
    • 2018-07-09
    • 2017-07-15
    • 2014-09-12
    • 2015-11-23
    • 2023-03-31
    相关资源
    最近更新 更多