| |
- From_File(filename)
- create a Table object from a file.
"-" means: read from stdin
This function returns a new Table object from a file or stdin.
parameters:
filename -- the name of a file containing the table. If the filename is
"-", the data is read from stdin. The first line should be
the heading with column names, all following lines should
contain the table numbers.
returns:
a new numpy structured array.
quirks:
numpy silently removes dashes ("-") from column names
- From_Lines(lines)
- create a Table object from a list of lines.
This function returns a new Table object from a list of lines.
parameters:
lines -- a list of strings representing the table. The first line should
be the heading with column names, all following lines should
contain the table numbers.
returns:
a new numpy structured array.
Here is an example:
>>> tab= From_Lines(["t x y","1 2 3","2 4 6"])
>>> print_(tab)
t x y
1.0 2.0 3.0
2.0 4.0 6.0
- averages(tab, filter_func=None, column_list=None)
- calculate the mean values of all columns.
This function calculates the mean values for all columns and all selected
rows. The selected rows are rows where filter_func returns True, when
applied to a dictionary with the values of the row. If filter_func is not
given, all rows are taken into account. This function returns a dictionary
with the mean value for each column.
parameters:
tab -- a numpy structured array
filter_func -- an optional function that is used to filter the lines. If
this function is not given, all lines are take into
account.
column_list -- if given, this specifies the names of the columns where
the function should be applied. It this parameter is
missing, the function is applied to all columns.
returns:
a dictionary with the mean values for all columns.
Here is an example:
>>> tab= numpy.zeros(4,dtype={"names":["t","x","y"],"formats":["f4","f4","f4"]})
>>> tab["t"]=[1,2,3,4]
>>> tab["x"]=[2,4,6,8]
>>> tab["y"]=[4,8,16,32]
>>> print_(tab)
t x y
1.0 2.0 4.0
2.0 4.0 8.0
3.0 6.0 16.0
4.0 8.0 32.0
>>> m= averages(tab)
>>> print("averages:\n",
... "\n".join([ "%s: %s" % (n,m[n]) for n in sorted(m.keys())]),
... sep='')
averages:
t: 2.5
x: 5.0
y: 15.0
>>> m= averages(tab, filter_func=lambda t,x,y:t%2==0)
>>> print("averages:\n",
... "\n".join([ "%s: %s" % (n,m[n]) for n in sorted(m.keys())]),
... sep='')
averages:
t: 3.0
x: 6.0
y: 20.0
>>> m= averages(tab, column_list=["t","y"])
>>> print("averages:\n",
... "\n".join([ "%s: %s" % (n,m[n]) for n in sorted(m.keys())]),
... sep='')
averages:
t: 2.5
y: 15.0
- combine(tab, other)
- combine two Tables, simply row by row, adds all columns.
This function combines two tables. Both tables must have the same
number of rows. The columns of both tables are taken together to create
a new table that is returned.
parameters:
tab -- a numpy structured array
other -- the other Table object
returns:
a new Table object.
Here is an example:
>>> tab1= numpy.zeros(3,dtype={"names":["t","x"],"formats":["f4","f4"]})
>>> tab1["t"]=[1,2,3]
>>> tab1["x"]=[2,4,6]
>>> tab2= numpy.zeros(3,dtype={"names":["a","b"],"formats":["f4","f4"]})
>>> tab2["a"]=[10,20,30]
>>> tab2["b"]=[20,40,60]
>>> print_(tab1)
t x
1.0 2.0
2.0 4.0
3.0 6.0
>>> print_(tab2)
a b
10.0 20.0
20.0 40.0
30.0 60.0
>>> print_(combine(tab1,tab2))
t x a b
1.0 2.0 10.0 20.0
2.0 4.0 20.0 40.0
3.0 6.0 30.0 60.0
- count(tab, filter_func)
- count all rows where filter_func returns True.
parameters:
tab -- a numpy structured array
filter_func -- an optional function that is used to filter the lines
returns:
the number of rows where filter_func returned True
Here is an example:
>>> tab= numpy.zeros(4,dtype={"names":["t","x","y"],"formats":["f4","f4","f4"]})
>>> tab["t"]=[1,2,3,4]
>>> tab["x"]=[2,4,6,8]
>>> tab["y"]=[4,8,16,32]
>>> print_(tab)
t x y
1.0 2.0 4.0
2.0 4.0 8.0
3.0 6.0 16.0
4.0 8.0 32.0
>>> count(tab,lambda t,x,y: x>=4)
3
>>> count(tab,lambda t,x,y: 2*x<y)
2
- derive(tab, derive_by, derive_these, new_names=None, keep_derive_by=False)
- calculate new columns with the derivative of values.
This function is used to create the derivative of one or more columns
by a given column. The derivatives are new columns in the new table
that is returned.
parameters:
tab -- a numpy structured array
derive_by -- the name of the column by which is derived
derive_these -- a list of column names that are derived
new_names -- a list of names for the new columns. If this
parameter is not given, the new names are created
by prepending a "d" to the original column names.
keep_derive_by -- if True, the column by which is derived is kept in
the new table.
returns:
a new Table object.
Here are some examples:
>>> tab= numpy.zeros(3,dtype={"names":["t","x","y"],"formats":["f4","f4","f4"]})
>>> tab["t"]=[0,2,10]
>>> tab["x"]=[2,4,6]
>>> tab["y"]=[4,8,16]
>>> print_(tab)
t x y
0.0 2.0 4.0
2.0 4.0 8.0
10.0 6.0 16.0
>>> print_(derive(tab,"t",["x","y"]))
dx dy
0.0 0.0
1.0 2.0
0.25 1.0
>>> print_(derive(tab,"t",["x","y"],keep_derive_by=True))
t dx dy
0.0 0.0 0.0
2.0 1.0 2.0
10.0 0.25 1.0
- derive_add(tab, derive_by, derive_these, new_names=None)
- calculate additional columns with the derivative of values.
This function is used to create the derivative of one or more columns
by a given column. The derivatives are added as new columns. As usual,
the original table is not modified but a new table is created.
parameters:
tab -- a numpy structured array
derive_by -- the name of the column by which is derived
derive_these -- a list of column names that are derived
new_names -- a list of names for the new columns. If this
parameter is not given, the new names are created by
prepending a "d" to the original column names.
returns:
a new Table object.
Here are some examples:
>>> tab= numpy.zeros(3,dtype={"names":["t","x","y"],"formats":["f4","f4","f4"]})
>>> tab["t"]=[0,2,10]
>>> tab["x"]=[2,4,6]
>>> tab["y"]=[4,8,16]
>>> print_(tab)
t x y
0.0 2.0 4.0
2.0 4.0 8.0
10.0 6.0 16.0
>>> print_(derive_add(tab, "t",["x","y"]))
t x y dx dy
0.0 2.0 4.0 0.0 0.0
2.0 4.0 8.0 1.0 2.0
10.0 6.0 16.0 0.25 1.0
>>> print_(derive_add(tab, "t",["x"],["velocity"]))
t x y velocity
0.0 2.0 4.0 0.0
2.0 4.0 8.0 1.0
10.0 6.0 16.0 0.25
- filter(tab, fun)
- filter a table by a function.
This function applies a function to each row of the table. If this
function returns True, the row is taken, otherwise the row is skipped.
All of the taken rows are taken to create a new table.
parameters:
tab -- a numpy structured array
fun -- this function is applied to the values of each row. All
values of a row are given to this function as named parameters.
The function should return a single boolean value.
returns:
a new table with the filtered rows.
Here are some examples:
>>> tab= numpy.zeros(4,dtype={"names":["t","x","y"],"formats":["f4","f4","f4"]})
>>> tab["t"]=[1,2,3,4]
>>> tab["x"]=[2,4,6,8]
>>> tab["y"]=[4,8,16,32]
>>> print_(tab)
t x y
1.0 2.0 4.0
2.0 4.0 8.0
3.0 6.0 16.0
4.0 8.0 32.0
>>> print_(filter(tab, lambda t,x,y: t!=2))
t x y
1.0 2.0 4.0
3.0 6.0 16.0
4.0 8.0 32.0
>>> print_(filter(tab, lambda t,x,y: t%2==0))
t x y
2.0 4.0 8.0
4.0 8.0 32.0
- fold(tab, fun, initial=None, filter_func=None)
- calculate a single value (or tuple) from the table.
This function can be used to create a value from the table by applying
a function to every column. This function gets the "initial" parameter
as a first parameter. All following parameters are named parameters one
for each row. The value the function returns is given as "initial"
parameters in the next call of the function where it gets the numbers
of the following row.
parameters:
tab -- a numpy structured array
fun -- the function. It must accept an anonymous first parameter
and a list of named parameters, one for each column in the
table.
filter_func -- an optional function that is used to filter the lines
where the fold function <fun> is applied. If this function
is given, <fun> is only applied to lines were filter_func
returns True.
returns:
a value that is given as anonymous first parameter to the next call
of the function.
Here are some examples:
>>> tab= numpy.zeros(4,dtype={"names":["t","x","y"],"formats":["f4","f4","f4"]})
>>> tab["t"]=[1,2,3,4]
>>> tab["x"]=[2,4,6,8]
>>> tab["y"]=[4,8,16,32]
>>> print_(tab)
t x y
1.0 2.0 4.0
2.0 4.0 8.0
3.0 6.0 16.0
4.0 8.0 32.0
>>> fold(tab, lambda s,t,x,y: s+t, 0)
10.0
>>> fold(tab, lambda s,t,x,y: s+x, 0)
20.0
>>> fold(tab, lambda s,t,x,y: s+y, 0)
60.0
>>> fold(tab, lambda s,t,x,y: s*t, 1)
24.0
>>> fold(tab, lambda s,t,x,y: s+x, 0, lambda t,x,y: t>2)
14.0
>>> fold(tab, lambda s,t,x,y: s*t, 1, lambda t,x,y: t!=3)
8.0
- fold_dict(tab, fun, initial=None, filter_func=None, column_list=None)
- apply a fold function to all columns of a table.
The fold function is called like this: fun(initial, field_value) for each
specified column in each filtered row. The value returned is passed as
<initial> parameter the next time the function is called for the same
column. The result is a dictionary that contains the latest <initial>
values for all specified columns.
parameters:
tab -- a numpy structured array
fun -- the function to call for each field. It should return a
single value.
initial -- the value that is passed to the function at the first time
it is called.
filter_func -- if given, this function specifies which rows to use. It is
called with a dictionary of all values for each row and
should return a boolean value. If it returns True, the row
is selected, otherwise it is skipped.
column_list -- if given, this specifies the names of the columns where
the function should be applied. It this parameter is
missing, the function is applied to all columns.
Here are some examples:
>>> tab= numpy.zeros(4,dtype={"names":["t","x","y"],"formats":["f4","f4","f4"]})
>>> tab["t"]=[1,2,3,4]
>>> tab["x"]=[2,4,6,8]
>>> tab["y"]=[4,8,16,32]
>>> print_(tab)
t x y
1.0 2.0 4.0
2.0 4.0 8.0
3.0 6.0 16.0
4.0 8.0 32.0
>>> d= fold_dict(tab, lambda s,x: min(s,x) if s is not None else x)
>>> for k in sorted(d.keys()):
... print(k,": ",d[k])
...
t : 1.0
x : 2.0
y : 4.0
>>> d= fold_dict(tab, lambda s,x: max(s,x) if s is not None else x)
>>> for k in sorted(d.keys()):
... print(k,": ",d[k])
...
t : 4.0
x : 8.0
y : 32.0
>>> d= fold_dict(tab, lambda s,x: min(s,x) if s is not None else x,
... filter_func= lambda t,x,y: t>2)
>>> for k in sorted(d.keys()):
... print(k,": ",d[k])
...
t : 3.0
x : 6.0
y : 16.0
- join_by(tab, other, key)
- join two tables by a key.
This function combines two tables by a key column that must be present
in both tables. It returns a new Table object that combines the columns
of both tables. Note that the key column is, of course, only taken
once.
parameters:
tab -- a numpy structured array
other -- the other Table object
key -- the name of the key column
returns:
a new Table object.
Here is an example:
>>> tab1= numpy.zeros(3,dtype={"names":["t","x"],"formats":["f4","f4"]})
>>> tab1["t"]=[1,2,3]
>>> tab1["x"]=[2,4,6]
>>> tab2= numpy.zeros(3,dtype={"names":["t","y"],"formats":["f4","f4"]})
>>> tab2["t"]=[1,2,3]
>>> tab2["y"]=[4,8,16]
>>> print_(tab1)
t x
1.0 2.0
2.0 4.0
3.0 6.0
>>> print_(tab2)
t y
1.0 4.0
2.0 8.0
3.0 16.0
>>> print_(join_by(tab1,tab2,"t"))
t x y
1.0 2.0 4.0
2.0 4.0 8.0
3.0 6.0 16.0
- map(tab, names, fun)
- calculate new columns and return a new table.
This function is used to calculate one or more numbers from the columns
of a table and create a new table with these numbers. The function
"fun" is called for each row of the table, all the numbers are given as
named parameters to the function. The function may return a single
number or a tuple of numbers. The names of the new columns are given in
the parameter names. Note that a property "_bag" is added to the
function, which is an empty dictionary at the first call. The function
can use this as a persistent data store to store values between it's
calls.
parameters:
tab -- a numpy structured array
names -- this is a list of strings that defines the names of the
new columns.
fun -- the function that is called to calculate the new
columns.
returns:
a new Table object.
Here is an example:
>>> tab= numpy.zeros(3,dtype={"names":["t","x","y"],"formats":["f4","f4","f4"]})
>>> tab["t"]=[1,2,3]
>>> tab["x"]=[2,4,6]
>>> tab["y"]=[4,8,16]
>>> print_(tab)
t x y
1.0 2.0 4.0
2.0 4.0 8.0
3.0 6.0 16.0
>>> print_(map(tab, ["sum","mul"],lambda t,x,y: (x+y,x*y)))
sum mul
6.0 8.0
12.0 32.0
22.0 96.0
- map_add(tab, new_names, fun)
- calculate additional columns and return a new table.
This function is used to calculate one or more numbers from the columns
of a table and add these numbers as additional columns to the table. As
usual, the original table is not modified but a new table is created
and returned. The function "fun" is called for each row of the table,
all the numbers are given as named parameters to the function. The
function may return a single number or a tuple of numbers. The names of
the additional columns are given in the parameter new_names. Note that
a property "_bag" is added to the function, which is an empty
dictionary at the first call. The function can use this as a persistent
data store to store values between it's calls.
parameters:
tab -- a numpy structured array
new_names -- this is a list of strings that defines the names of the
new columns.
fun -- the function that is called to calculate the new
columns.
returns:
a new Table object.
Here is an example:
>>> tab= numpy.zeros(3,dtype={"names":["t","x","y"],"formats":["f4","f4","f4"]})
>>> tab["t"]=[1,2,3]
>>> tab["x"]=[2,4,6]
>>> tab["y"]=[4,8,16]
>>> print_(map_add(tab, ["t+x","t*x"],lambda t,x,y:(t+x,t*x)))
t x y t+x t*x
1.0 2.0 4.0 3.0 2.0
2.0 4.0 8.0 6.0 8.0
3.0 6.0 16.0 9.0 18.0
- numpy_seterr(*args, **kwargs)
- make function seterr from numpy available.
- print_(tab, sep=' ', formats=None, justifications=None)
- print the table.
This function prints the table to the console. It simply calls the
method to_lines() and prints the lines this function returns.
parameters:
tab -- a numpy structured array
sep -- the string that separates rows, the default is a
single space.
formats -- an array of strings that specify the formats for
each column. The formats are the ones that the
python "%" operator supports. The default is a "%s"
for all columns. If the number of elements in this
list is smaller than the number of columns, the
last format string in the list is taken for all
remaining columns. By this it is sufficient in many
cases to provide an list with just a single format
string that will then be used for all columns
justifications -- This list of characters specifies the justification
for each column. Known justification characters are
"L" for left, "C" for center and "R" for right
justification. If the number of elements in this
list is smaller than the number of columns, the
last justification character in the list is taken
for all remaining columns. By this it is sufficient
in many cases to provide an list with just a single
justification character that will then be used for
all columns
Here are some examples, more are at to_lines:
>>> tab= numpy.zeros(3,dtype={"names":["time","measured-x","measured-y"],
... "formats":["f4","f4","f4"]})
>>> tab["time"]=[1,2,3]
>>> tab["measured-x"]=[2.2,4.4,6.6]
>>> tab["measured-y"]=[4.45,8.55,16.65]
>>> print_(tab, sep="|")
time|measured-x|measured-y
1.0 |2.2 |4.45
2.0 |4.4 |8.55
3.0 |6.6 |16.65
>>> print_(tab, sep="|",justifications=["R","R","R"])
time|measured-x|measured-y
1.0| 2.2| 4.45
2.0| 4.4| 8.55
3.0| 6.6| 16.65
- rename_by_dict(tab, newname_dict)
- create a new Table, change the names of columns with a dict.
This method creates a new Table object where some or all of the columns may
have been renamed. The mapping defined in the given dictionary is not
required to be complete. Column names not found in the dictionary remain
unchanged.
parameters:
tab -- a numpy structured array
newname_dict -- a dictionary mapping old column names to new column names.
returns:
a new Table object where the columns are renamed.
Here is an example:
>>> tab= numpy.zeros(3,dtype={"names":["t","x","y"],"formats":["f4","f4","f4"]})
>>> tab["t"]=[1,2,3]
>>> tab["x"]=[2,4,6]
>>> tab["y"]=[4,8,16]
>>> print_(tab)
t x y
1.0 2.0 4.0
2.0 4.0 8.0
3.0 6.0 16.0
>>> print_(rename_by_dict(tab, {"t":"T","y":"New"}))
T x New
1.0 2.0 4.0
2.0 4.0 8.0
3.0 6.0 16.0
- rename_by_function(tab, fun)
- create a new Table, change the names of columns with a function.
This method creates a new Table object where some or all of the columns may
have been renamed. The new names are determined by applying the given
function to each of the old column names.
parameters:
tab -- a numpy structured array
fun -- a function mapping old column names to new column names.
returns:
a new Table object where the columns are renamed.
Here is an example:
>>> tab= numpy.zeros(3,dtype={"names":["t","x","y"],"formats":["f4","f4","f4"]})
>>> tab["t"]=[1,2,3]
>>> tab["x"]=[2,4,6]
>>> tab["y"]=[4,8,16]
>>> print_(tab)
t x y
1.0 2.0 4.0
2.0 4.0 8.0
3.0 6.0 16.0
>>> print_(rename_by_function(tab, lambda n: n+"_new"))
t_new x_new y_new
1.0 2.0 4.0
2.0 4.0 8.0
3.0 6.0 16.0
- rm_columns(tab, fieldlist)
- remove columns from the table.
This function removes columns from the table and returns a new table
with the remaining columns.
parameters:
tab -- a numpy structured array
fieldlist -- the list of columns that are to be removed.
returns:
the new table.
Here is an example:
>>> tab= numpy.zeros(3,dtype={"names":["t","a","b","c"],"formats":["f4","f4","f4","f4"]})
>>> tab["t"]=[1,2,3]
>>> tab["a"]=[2,4,6]
>>> tab["b"]=[3,6,9]
>>> tab["c"]=[4,8,12]
>>> print_(rm_columns(tab, ["a","c"]))
t b
1.0 3.0
2.0 6.0
3.0 9.0
- sample_standard_deviation(tab, filter_func=None, column_list=None)
- calculate the sample standard deviation of all columns.
This function calculates the mean values for all columns and all selected
rows. The selected rows are rows where filter_func returns True, when
applied to a dictionary with the values of the row. If filter_func is not
given, all rows are taken into account. This function returns a dictionary
with the mean value for each column.
parameters:
tab -- a numpy structured array
filter_func -- an optional function that is used to filter the lines. If
this function is not given, all lines are take into
account.
column_list -- if given, this specifies the names of the columns where
the function should be applied. It this parameter is
missing, the function is applied to all columns.
returns:
a dictionary with the mean values for all columns.
Here is an example:
>>> tab= numpy.zeros(4,dtype={"names":["t","x","y"],"formats":["f4","f4","f4"]})
>>> tab["t"]=[1,2,3,4]
>>> tab["x"]=[2,4,6,8]
>>> tab["y"]=[4,8,16,32]
>>> print_(tab)
t x y
1.0 2.0 4.0
2.0 4.0 8.0
3.0 6.0 16.0
4.0 8.0 32.0
>>> d=sample_standard_deviation(tab)
>>> for k in sorted(d.keys()):
... print("%s: %6.2f" % (k,d[k]))
...
t: 1.29
x: 2.58
y: 12.38
>>> d=sample_standard_deviation(tab,filter_func=lambda t,x,y:t>2)
>>> for k in sorted(d.keys()):
... print("%s: %6.2f" % (k,d[k]))
...
t: 0.71
x: 1.41
y: 11.31
>>> d=sample_standard_deviation(tab,column_list=["t","y"])
>>> for k in sorted(d.keys()):
... print("%s: %6.2f" % (k,d[k]))
...
t: 1.29
y: 12.38
- str_(tab)
- return the table as a human readable simple string.
parameters:
tab -- a numpy structured array
This function returns the table as a single text representing the
table.
Here is an example:
>>> tab= numpy.zeros(3,dtype={"names":["t","x","y"],"formats":["f4","f4","f4"]})
>>> tab["t"]=[1,2,3]
>>> tab["x"]=[2,4,6]
>>> tab["y"]=[4,8,16]
>>> print(str_(tab))
t x y
1.0 2.0 4.0
2.0 4.0 8.0
3.0 6.0 16.0
- sums(tab, filter_func=None, column_list=None)
- calculate sums of columns and number of rows.
This function calculates the number of rows and the sum of columns for a
given table. It returns the number of rows where filter_func returned True
and a dictionary with the sums of values for that rows for each column. If
filter_func is omitted, all rows are taken into account.
parameters:
tab -- a numpy structured array
filter_func -- an optional function that is used to filter the lines. If
this function is not given, all lines are take into
account.
column_list -- if given, this specifies the names of the columns where
the function should be applied. It this parameter is
missing, the function is applied to all columns.
returns:
a tuple consisting of the number of rows where filter_func returned True
and a dictionary with the sum of values for all specified columns.
Here is an example:
>>> tab= numpy.zeros(4,dtype={"names":["t","x","y"],"formats":["f4","f4","f4"]})
>>> tab["t"]=[1,2,3,4]
>>> tab["x"]=[2,4,6,8]
>>> tab["y"]=[4,8,16,32]
>>> print_(tab)
t x y
1.0 2.0 4.0
2.0 4.0 8.0
3.0 6.0 16.0
4.0 8.0 32.0
>>> s= sums(tab)
>>> print("rows:",s[0])
rows: 4
>>> for k in sorted(s[1].keys()):
... print("sum(%s): %s" % (k,s[1][k]))
...
sum(t): 10.0
sum(x): 20.0
sum(y): 60.0
>>> s=sums(tab, filter_func=lambda t,x,y:t%2==0)
>>> print("rows:",s[0])
rows: 2
>>> for k in sorted(s[1].keys()):
... print("sum(%s): %s" % (k,s[1][k]))
...
sum(t): 6.0
sum(x): 12.0
sum(y): 40.0
>>> s=sums(tab, column_list=["t","y"])
>>> print("rows:",s[0])
rows: 4
>>> for k in sorted(s[1].keys()):
... print("sum(%s): %s" % (k,s[1][k]))
...
sum(t): 10.0
sum(y): 60.0
- take_columns(tab, row_list)
- create a new Table, take columns from the list.
This method can be used to select only some of the rows of a table and
to reorder rows of the table. It returns a new Table object.
parameters:
tab -- a numpy structured array
row_list -- a list of rows to select. Note that the order of the rows
matters with respect to the method print_ (see print_).
Here are some examples:
>>> tab= numpy.zeros(4,dtype={"names":["t","x","y"],"formats":["f4","f4","f4"]})
>>> tab["t"]=[1,2,3,4]
>>> tab["x"]=[2,4,6,8]
>>> tab["y"]=[4,8,16,32]
>>> print_(tab)
t x y
1.0 2.0 4.0
2.0 4.0 8.0
3.0 6.0 16.0
4.0 8.0 32.0
>>> print_(take_columns(tab, ["x","y"]))
x y
2.0 4.0
4.0 8.0
6.0 16.0
8.0 32.0
>>> print_(take_columns(tab, ["x","y","t"]))
x y t
2.0 4.0 1.0
4.0 8.0 2.0
6.0 16.0 3.0
8.0 32.0 4.0
- to_lines(tab, sep=' ', formats=None, justifications=None)
- pretty-print Table.
This function converts a table to a list of lines. This function is
also the base for the __str__ and the print_ function.
parameters:
tab -- a numpy structured array
sep -- the string that separates rows, the default is a
single space.
formats -- an array of strings that specify the formats for
each column. The formats are the ones that the
python "%" operator supports. The default is a "%s"
for all columns. If the number of elements in this
list is smaller than the number of columns, the
last format string in the list is taken for all
remaining columns. By this it is sufficient in many
cases to provide an list with just a single format
string that will then be used for all columns
justifications -- This list of characters specifies the justification
for each column. Known justification characters are
"L" for left, "C" for center and "R" for right
justification. The default if no justification is given
is to use left justification. If the number of elements
in this list is smaller than the number of columns, the
last justification character in the list is taken for
all remaining columns. By this it is sufficient in many
cases to provide an list with just a single
justification character that will then be used for all
columns
returns:
a list of lines representing the table.
Here are some examples:
>>> tab= numpy.zeros(3,dtype={"names":["time","measured-x","measured-y"],
... "formats":["f4","f4","f4"]})
>>> tab["time"]=[1,2,3]
>>> tab["measured-x"]=[2.2,4.4,6.6]
>>> tab["measured-y"]=[4.45,8.55,16.65]
>>> print("\n".join(to_lines(tab)))
time measured-x measured-y
1.0 2.2 4.45
2.0 4.4 8.55
3.0 6.6 16.65
>>> print("\n".join(to_lines(tab, sep="|")))
time|measured-x|measured-y
1.0 |2.2 |4.45
2.0 |4.4 |8.55
3.0 |6.6 |16.65
>>> print("\n".join(to_lines(tab, sep="|",
... formats=["%5.2f","%20.3f","%6.4f"])))
time |measured-x |measured-y
1.00| 2.200|4.4500
2.00| 4.400|8.5500
3.00| 6.600|16.6500
>>> print("\n".join(to_lines(tab, sep="|",
... formats=["%5.2f","%20.3f","%6.4f"],
... justifications=["R","L","C"])))
time|measured-x |measured-y
1.00| 2.200| 4.4500
2.00| 4.400| 8.5500
3.00| 6.600| 16.6500
>>> print("\n".join(to_lines(tab, sep="|",
... formats=["%5.2f","%6.3f","%6.4f"],
... justifications=["R","L","C"])))
time|measured-x|measured-y
1.00| 2.200 | 4.4500
2.00| 4.400 | 8.5500
3.00| 6.600 | 16.6500
|