Appending to file using savetxt

后端 未结 2 342
我寻月下人不归
我寻月下人不归 2021-01-19 04:02

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
         


        
2条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-19 04:38

    Use open() with the 'append' mode, and pass the stream to the savetxt method:

    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)
    

提交回复
热议问题