On 10.05.2013, at 2:51PM, Daniele Nicolodi <dani...@grinta.net> wrote:

> If you wish to format numpy arrays preceding them with a variable name,
> the following is a possible solution that gives the same formatting as
> in your example:
> 
> import numpy as np
> import sys
> 
> def format(out, v, name):
>    header = "{} = ".format(name)
>    out.write(header)
>    np.savetxt(out, v, fmt="%d", delimiter=", ",
>               newline="\n" + " " * len(header))
>    out.write("\n")
> 
> IL = np.array([range(5), ] * 5)
> format(sys.stdout, IL, "IL")

That is a quite ingenuous way to use savetxt functionality to write that extra 
column!

Only two comments:

Don't call that function "format", as it would mask the 'format' builtin!

In the present version it will only work with a file handle; to print it a to 
file you would need
to pass it as fformat(open(fname, 'a'), … or check for that case inside the 
function.

Cheers,
                                                Derek
 


_______________________________________________
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion

Reply via email to