Reading formatted files
- The below example shows a simple function for reading formatted data files:
########################################################### ## This function is defined in the module fmtfiles.read_fmt ########################################################### def read_column(fname): f = open(fname) lines = f.readlines() f.close() data = [] for line in lines: if line[0] == '#': pass else: tmp = line.split() #print tmp d = map(float,tmp) #print d data.append(d) data = num.array(data) return num.transpose(data) ############################################################# - To import this function
pds>>from fmtfiles.read_fmt import read_column as read_dat
- Heres a simple data file (stuff.dat)
# x, y 1 2 3 4 5 6
Read it into the variable dat
pds>dat = read_dat("stuff.dat") pds>dat array([[ 1., 3., 5.], [ 2., 4., 6.]]) pds>dat[0] array([ 1., 3., 5.]) pds>dat[1] array([ 2., 4., 6.]) pds>
Writing formatted files
- We can write this same array to a file using the following sequence of commands
- Below we create a new file (data.out) in the current working directory
pds>f = open('data.out','w') pds>for j in range(len(dat[0])): ...> l = "%6.3f, %6.3f\n" % (dat[0,j],dat[1,j]) ...> f.write(l) ...> pds>f.close() pds>more data.out 1.000, 2.000 3.000, 4.000 5.000, 6.000 pds>
Save/Restore
- The save command / function tries to save data in a python pickle (a python archive of sorts). The save command by default tries to figure out what is 'data' versus modules and funcitons etc.. in other words save just tries to save things that arent defined in modules. However, the pickle is finicky. Not all data can be pickled, and it may not behave nicely down the road if a module that uses a particular data type has changed and expects something different from your data object... Therefore, it is always safest to write data in formatted files, but that can be a pain, so the pickle is an option...
This example saves all the 'data to a default file name that is the time stamp -> save_2009_11_10_2200.sav.
We then clear all the variables and restore the variables back again.
pds>show : : ==== Variables ==== dat j l x pds>save pds>ls save_2009_11_10_2200.sav pds>clear pds>restore save_2009_11_10_2200.sav pds>show : : ==== Variables ==== dat j l x