data file is not including the last line in python -
when run code dumps 'values.dat'
file after when try load these data variable can proceed further calculation....the variable c
loading data the last line.
fs=glob.glob('*all_strain_rate_data_files/srate*.txt') fil in fs: sr=float(fil.split('srate')[1].split('.txt')[0]) xy=loadtxt(fil) y=xy[:,1] avg=mean(y) n_f= open('values.dat' , 'a') n_f.writelines(str(sr)+'\t'+str(avg)+'\n') n_f.close c = loadtxt('values.dat') print c
the .dat
file has following data
0.001 -0.000476901658291 0.005 -0.000459584857803 0.01 -0.000455371401619 0.02 -0.000454457184105 0.03 -0.000363427058283 0.04 -0.000425998443982 0.05 -0.000479518449808 0.06 -0.00044039265987 0.07 -0.000428992925364 0.08 -0.000479521006314 0.09 -0.000346052124686 0.5 -0.000419424003512 , print of c ........gives [[ 0.001 -0.0004769 ] [ 0.005 -0.00045958] [ 0.01 -0.00045537] [ 0.02 -0.00045446] [ 0.03 -0.00036343] [ 0.04 -0.000426 ] [ 0.05 -0.00047952] [ 0.06 -0.00044039] [ 0.07 -0.00042899] [ 0.08 -0.00047952] [ 0.09 -0.00034605]
n.b : if following in python script it loads last line.
c = loadtxt('values.dat') print c
so if can work in separate script why not reading last line in same script ?
you unnecessarily opening , closing file in loop. should open out of loop, , close once done:
fs=glob.glob('*all_strain_rate_data_files/srate*.txt') n_f= open('values.dat' , 'a') fil in fs: sr=float(fil.split('srate')[1].split('.txt')[0]) xy=loadtxt(fil) y=xy[:,1] avg=mean(y) n_f.writelines(str(sr)+'\t'+str(avg)+'\n') n_f.close c = loadtxt('values.dat') print c
Comments
Post a Comment