使用savetxt附加到文件 [英] Appending to file using savetxt

查看:76
本文介绍了使用savetxt附加到文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将数组b和c附加到file.txt.默认行为是覆盖,我找不到任何有关更改此内容的文档.

I'm trying to append array b and c to file.txt. The default behavior is to overwrite and I cant find any documentation on changing this.

import numpy as np
a = np.array([1.2, 2.3, 4.5])
b = np.array([6.7, 8.9, 10.11])
c = np.array([12.13, 14.15, 16.17])

np.savetxt('file.txt', a, fmt='%1.3f', newline=", ")

谢谢

推荐答案

使用 open() 的追加"模式,并将流传递给savetxt方法:

with open("test.txt", "ab") as f:
    numpy.savetxt(f, a)

编辑:要添加新行,或执行以下操作:

Edit: To add a new line, or whatever:

with open("test.txt", "ab") as f:
    f.write(b"\n")
    numpy.savetxt(f, a)

这篇关于使用savetxt附加到文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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