python - Add header to np.matrix -
i trying export numpy matrix ascii format, want add header first.
my code concept this:
- import ascii file np.ndarray, matrix
- take header of (first 6 rows). header contains both float values , characters
- take rows of not header (from rows 6 last row), giving array b
- apply functions on b
- save ascii in form: header(a) + b
i tried following:
try 1:
import numpy np = np.genfromtxt('......input\chm_plot_1.txt', dtype=none, delimiter='\t') header = a[0:6] b = a[6:] mat_out = np.concatenate([a,b]) np.savetxt('........out.txt', mat_out, delimiter='\t')
,but gives error:
typeerror: mismatch between array dtype ('|s3973') , format specifier ('%.18e')
try 2:
import numpy np = np.genfromtxt('......input\chm_plot_1.txt', dtype=none, delimiter='\t') header = a[0:6] headers = np.vstack(header) head_list = headers.tolist() head_str = ''.join(str(v) v in head_list) b = a[6:] np.savetxt('\out.txt', b, header = head_str, delimiter='\t')
,which gives same error:
typeerror: mismatch between array dtype ('|s3973') , format specifier ('%.18e')
try 3:
import numpy np import linecache = np.genfromtxt('.............\input\chm_plot_1.txt', dtype=none, delimiter='\t') line1 = linecache.getline('.............input\chm_plot_1.txt', 1) line2 = linecache.getline('.............input\chm_plot_1.txt', 2) line3 = linecache.getline('.............input\chm_plot_1.txt', 3) line4 = linecache.getline('.............input\chm_plot_1.txt', 4) line5 = linecache.getline('.............input\chm_plot_1.txt', 5) line6 = linecache.getline('.............input\chm_plot_1.txt', 6) header2 = line1 header2 += line2 header2 += line3 header2 += line4 header2 += line5 header2 += line6 b = a[6:] np.savetxt('........\out.txt', b , header = header2, delimiter='\t')
, gives me same error:
typeerror: mismatch between array dtype ('|s3973') , format specifier ('%.18e')
the array has first lines this:
print a[0:8] #starting row 6, rows have 100+ values, header first 6 rows
['ncols 371' 'nrows 435' 'xllcorner 520298.0053' 'yllcorner 436731.3065' 'cellsize 1' 'nodata_value -9999' '16.52002 15.90161 15.96692 20.32922 20.59827 18.28137 18.83533 17.66 ....... '13.16687 17.09497 7.309204 20.83655 19.05078 17.68591 17.88464 ...... ']
any appreciated! :)
edit: uploaded sample input data (chm_plot_1.txt). link below: http://we.tl/mjgbe4qirm
edit2: following answer, problem inserts "#" character @ beginning of header lines, in image below below. also, there 1 supplementary line, 7th one, should removed.
edit 3: think error
valueerror: invalid literal float()
is due different formats of data in sample vs full files. although both .txt, arranged differently, in picture below.
the problem header has not same format data.
a way solve : treat header normal file text, , data numeric.
with open('chm_plot_1_sample.txt') f : header="".join([f.readline() in range(6)])[:-1] a=np.loadtxt('chm_plot_1_sample.txt',delimiter='\t',skiprows=6) a=a/2 # treatement np.savetxt('out.txt',a,delimiter='\t',header=header,comments='')
Comments
Post a Comment