0

I am trying to save a 100x100x100 array of integers to a file with the date and time it was saved as a header. It doesn't need to be human-readable (except for the time-stamp header) so I was planning to use numpy.save(), take one slice at a time and save it to the file, but this does not append to the end of the file, it overwrites each time so the file only ends up containing the last slice.

Is there something like save() or savetxt() which appends to a file rather that overwrites?

Note: if it makes it easier, could I put the date/time into the filename when it saves instead of into the header?

My current attempt looks something like this:

with open("outfile.txt",'w') as mfile:
    mfile.write(strftime("%x %X\n"))
for i in range(len(x)):
    np.savetxt("outfile.txt",x[i])
6
  • 1
    Could you please show the code you use to save your data? Thanks! numpy.save takes a file as first argument, did you open with an 'a' flag ? Commented Jul 20, 2015 at 13:49
  • Ok, I have done that and it now saves all the slices, but I can't use np.save() if I want the date/time as a header in the file as it confuses the np.load() function when I want to read it back in. Commented Jul 20, 2015 at 13:58
  • Aha! Figured out how to do it if I read in the header first with readline(), then use np.load(), but now load() seems to only pick up one slice... can it deal with 3D data or do I have to read each slice in and then stack them? Commented Jul 20, 2015 at 14:04
  • are you using python 2 or 3 ? Commented Jul 20, 2015 at 14:29
  • 1
    You could also use reshape or flatten on your array in order to obtain low-dimensional versions. You need to remember your array shape while loading however (but that is probably the case anyway). Commented Jul 20, 2015 at 14:35

3 Answers 3

1

Use the 'a'flag to append to a file. numpy.savetxt takes an array structure as input, so we need to reshape it.

p,q,r = x.shape
with open("outfile.txt",'ab') as mfile:
    header = strftime("%x %X\n")
    np.savetxt(mfile, x.reshape(p*q*r), header=header)
Sign up to request clarification or add additional context in comments.

2 Comments

I think you would like to have that for block deeper indented.
Uh, did not see that when I wrote my comment
1

I'm a fan of Pickles :)

import cPickle
import time
import numpy as np

arr = np.array(xrange(100)).reshape(10,10)

#write pickle file
with open('out.p', 'wb') as f:
    t = time.asctime()
    cPickle.dump(t, f, cPickle.HIGHEST_PROTOCOL)
    cPickle.dump(arr, f, cPickle.HIGHEST_PROTOCOL)

#read pickle file
with open('out.p', 'rb') as f:
    t = cPickle.load(f)
    arr = cPickle.load(f)

Comments

0

With the help of @galath's advice (although actually it doesn't seem to matter if I use 'a' or 'w' flag now...), I have figured out the following method using np.save/load and having the date as a header:

outfile="out.npy"

with open(outfile,'w') as mfile:
    mfile.write(strftime("%x %X\n"))      #write date and time as header
    for i in range(len(x)):
        np.save(mfile,x[i])               #save each slice

readx=np.zeros((len(x),len(y),len(z)) #empty array to be filled from file    

with open(outfile,'r') as mf:                #reopen file (for reading in)
    dt=mf.readline()                      #read date/time header
    for i in range(len(x)):
        readx[i,:,:]=np.load(mf)          #read in each slice and save it to array

If anyone has a more elegant solution feel free to share, but this way does what I need for now.

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.