This module provide the function d2f. The aim of d2f is to easily write block data into a
file with a given format and in one line. Maybe d2f looks like the fortran command write.
The up to date version is now on github : data2file
The main advantage of d2f is that you can give the format in a compact way and it avoids redondant
syntax. The syntax for format specification is the folowing :
as an example is clearer, this syntax
means that you want d2f to write two integers on four columns, then four strings on ten
columns and then height floating numbers on height columns with three digit after the comma.
d2f(blockdata [, name[, fmt[, header[, overwrite]]]])
- blockdata (list) : list of data, each element of the list in the first direction will be written on the same line.
- name (string) : output file name. This could be a path
- fmt (string) : format specification, example : ‘2x4d;4x10s;8×8.3f’
- header (string) : will be writtend at the top of the output file
- overwrite (bool) : if True, if the output file exists it will be overwritten
Assume you have three list objects, l1, l2 and l3 and you want to print l1 on the first column, l2 on the second column and l3 on the third column of an output file called
"out.dat". First you have to create a block data object. For that the easiest is to use
zip(l1, l2, l3). Assume l1 is an integer, and l2 and l3 are floating numbers. You will use
fmt="1x5d;2xf10.4" in order to write l1 as an integer on 5 columns and l2 and l3 as floats on 10 column with 4 digit after the comma. Thus simpliest syntax is :
>>> d2f(zip(l1, l2, l3), fmt = "1x5d;2xf10.4")
If you want to precise a file name :
>>> d2f(zip(l1, l2, l3), name = "output.dat", fmt = "1x5d;2xf10.4")