python - Add a Two Line Comment to Table Before Saving with Numpy savetxt -


here python 3 code giving me problem:

name_in_stk = name+'.out.abs.stk' data_stk = np.loadtxt(name_in_stk, skiprows=0) wavelength = [] ext_coef = [] ext_coeff = [row[1] row in data_stk ] wavelength = 1e7/np.array([row[0] row in data_stk ])  dataout = [] dataout = np.column_stack((wavelength,ext_coeff))  np.savetxt(name+'.stk',dataout, fmt=('%5.1f','%5.4e')) 

the output is:

435.6 1.8225e+04 396.7 3.2189e+04 333.8 3.7765e+03 325.2 4.6922e+04 315.5 1.0923e+05 307.0 9.9065e+02 296.4 1.3264e+03 288.2 5.6207e+04 282.8 3.4048e+04 266.1 2.5265e+04 

the question haven't been able answer, althought have searched, :

how add 2 line header table before saving it?

np.savetxt has option specify header, not (as expected) specifying list of column names corresponding columns want save, specifying string placed start of file, commented out #. there no option create 2 headers, since savetxt wants string header, makes thing easier can split header string 2 lines \n.

a minimal example:

import numpy np  data = np.random.random(10).reshape(5,2)*100 header = '{0:^5s} {1:^7s}\n{2:^5s} {3:^7s}'.format('a', 'b', 'c', 'd') np.savetxt('test', data, fmt=('%5.1f','%5.4e'), header=header) 

produces this:

#       b    #  c      d     39.0 7.4369e+01  87.2 8.7414e+01  50.0 7.7876e+01  23.9 8.2554e+01  87.7 8.9955e+01 

as side note: can individual columns loaded np.loadtxt e.g. ext_coeff = data_stk[:,1], , in line that, calculate wavelength wavelength = 1e7/data_stk[:,0]. if dealing large data sets, speed things quite bit rid of manual loops (.. row in ..). file million lines, speedup of factor 60.


Comments

Popular posts from this blog

python - How to insert QWidgets in the middle of a Layout? -

python - serve multiple gunicorn django instances under nginx ubuntu -

module - Prestashop displayPaymentReturn hook url -