0

I want to export numpy arrays as csv files but I may have files with the same names in my folder. So, I want to replace old files with new ones. I am using the following method:

arr=np.array([[1., 2.], [2., 1.]])
np.savetxt('arr.csv', arr, delimiter=',')

But this method overwrites rows of the existing arr.csv file in my folder. Is there a way to completely replace the old file? I do appreciate any help in advance.

2
  • 1
    What is the difference? The file will be overwritten, no? Commented Jul 20, 2021 at 9:07
  • Dear @Corralien, the difference is that my previous csv file may have 10 rows. Then new one may have only 8 rows. this method will rewrite the first 8 rows of old one and two last rows will remain there. I I could replace the file, then the problem of that two extra rows will be solved. Commented Jul 20, 2021 at 9:12

1 Answer 1

1

np.savetxt (numpy=='1.20.3') overwrites the whole file:

arr = np.random.random((3, 2))
np.savetxt('arr.csv', arr, delimiter=',')
>>> %cat arr.csv
2.123431629646361785e-01,6.914800473216878851e-01
9.544549349895192769e-01,6.749508363116073495e-01
2.238126343800779239e-01,3.919697864527211806e-01
arr = np.random.random((1, 2))
np.savetxt('arr.csv', arr, delimiter=',')
>>> %cat arr.csv
4.355318781801621464e-01,4.574856378543381563e-01
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.