nws_tools.printdata

nws_tools.printdata(data, leadrow, leadcol, fname=None)[source]

Pretty-print/-save array-like data

Parameters:

data : NumPy 2darray

An M-by-N array of data

leadrow : Python list or NumPy 1darray

List/array of length N or N+1 providing labels to be printed in the first row of the table (strings/numerals or both). See Examples for details

leadcol : Python list or NumPy 1darray

List/array of length M providing labels to be printed in the first column of the table (strings/numerals or both)

fname : str

Name of a csv-file (with or without extension .csv) used to save the table (WARNING: existing files will be overwritten!). Can also be a path + filename (e.g., fname=’path/to/file.csv’). By default output is not saved.

Returns:

Nothing : None

See also

texttable
a module for creating simple ASCII tables (currently available at the Python Package Index)

Notes

Uses the texttable module to print results

Examples

>>> import numpy as np
>>> data = np.random.rand(2,3)
>>> row1 = ["a","b",3]
>>> col1 = np.arange(2)
>>> printdata(data,row1,col1)
+--------------------+--------------------+--------------------+--------------------+
|                    |         a          |         b          |         3          |
+====================+====================+====================+====================+
| 0                  | 0.994018537964     | 0.707532139166     | 0.767497407803     |
+--------------------+--------------------+--------------------+--------------------+
| 1                  | 0.914193045048     | 0.758181936461     | 0.216752553325     |
+--------------------+--------------------+--------------------+--------------------+
>>> row1 = ["labels"] + row1
>>> printdata(data,row1,col1,fname='dummy')
+--------------------+--------------------+--------------------+--------------------+
|       labels       |         a          |         b          |         3          |
+====================+====================+====================+====================+
| 0                  | 0.994018537964     | 0.707532139166     | 0.767497407803     |
+--------------------+--------------------+--------------------+--------------------+
| 1                  | 0.914193045048     | 0.758181936461     | 0.216752553325     |
+--------------------+--------------------+--------------------+--------------------+
>>> cat dummy.csv
labels, a, b, 3
0,0.994018537964,0.707532139166,0.767497407803
1,0.914193045048,0.758181936461,0.216752553325