tableSile¶
-
class
sisl.io.table.
tableSile
(filename, mode='r', comment=None, *args, **kwargs)[source]¶ ASCII tabular formatted data
A generic table data which will easily accommodate the most common write-outs of data
Examples
>>> a = np.arange(6).reshape(3, 2) >>> tbl = tableSile('test.dat', 'w') >>> tbl.write_data(a) >>> print(''.join(open('test.dat').readlines())) 0.00000e+00 2.00000e+00 4.00000e+00 1.00000e+00 3.00000e+00 5.00000e+00
>>> a = np.arange(6).reshape(3, 2) >>> tbl = tableSile('test.dat', 'w') >>> tbl.write_data(a, comment='Hello') >>> print(''.join(open('test.dat').readlines())) # Hello 0.00000e+00 2.00000e+00 4.00000e+00 1.00000e+00 3.00000e+00 5.00000e+00
>>> a = np.arange(6).reshape(3, 2) >>> tbl = tableSile('test.dat', 'w') >>> tbl.write_data(a, header=['x', 'y']) >>> print(''.join(open('test.dat').readlines())) #x y 0.00000e+00 2.00000e+00 4.00000e+00 1.00000e+00 3.00000e+00 5.00000e+00
>>> a = np.arange(6).reshape(3, 2) >>> tbl = tableSile('test.dat', 'w') >>> tbl.write_data(a.T) >>> print(''.join(open('test.dat').readlines())) 0.00000e+00 1.00000e+00 2.00000e+00 3.00000e+00 4.00000e+00 5.00000e+00
>>> x = np.arange(4) >>> y = np.arange(8).reshape(2, 4) + 4 >>> tbl = tableSile('test.dat', 'w') >>> tbl.write_data(x, y) >>> print(''.join(open('test.dat').readlines())) 0.00000e+00 4.00000e+00 8.00000e+00 1.00000e+00 5.00000e+00 9.00000e+00 2.00000e+00 6.00000e+00 1.00000e+01 3.00000e+00 7.00000e+00 1.10000e+01
Attributes
base_file
File of the current Sile
file
File of the current Sile
Methods
__init__
(filename[, mode, comment])Initialize self. dir_file
([filename])File of the current Sile
exist
()Query whether the file exists read
(*args, **kwargs)Generic read method which should be overloaded in child-classes read_data
(*args, **kwargs)Read tabular data from the file. write
(*args, **kwargs)Generic write method which should be overloaded in child-classes write_data
(*args, **kwargs)Write tabular data to the file with optional header. -
base_file
¶ File of the current
Sile
-
dir_file
(filename=None)¶ File of the current
Sile
-
exist
()¶ Query whether the file exists
-
file
¶ File of the current
Sile
-
read
(*args, **kwargs)¶ Generic read method which should be overloaded in child-classes
Parameters: - kwargs :
keyword arguments will try and search for the attribute
read_<>
and call it with the remaining**kwargs
as arguments.
-
read_data
(*args, **kwargs)[source]¶ Read tabular data from the file.
Parameters: - columns : list of int, optional
only return the indices of the columns that are provided
- delimiter : str, optional
the delimiter used in the file, will automatically try to guess if not specified
- ret_comment : bool, optional
also return the comments at the top of the file (if queried)
- ret_header : bool, optional
also return the header information (if queried)
- comment : str, optional
lines starting with this are discarded as comments
-
write
(*args, **kwargs)¶ Generic write method which should be overloaded in child-classes
Parameters: - **kwargs :
keyword arguments will try and search for the attribute write_ and call it with the remaining
**kwargs
as arguments.
-
write_data
(*args, **kwargs)[source]¶ Write tabular data to the file with optional header.
Parameters: - *args : array_like or list of array_like
the different columns in the tabular data. This may either be several 1D data, or 2D arrays. Internally the data is stacked via
numpy.vstack
and for any dimension higher than 2 it gets separated by two newline characters (like gnuplot acceptable data).- fmt : str, optional
The formatting string, defaults to
.5e
.- fmts : str, optional
The formatting string (for all columns), defaults to
fmt * len(args)
. fmts has precedence over fmt.- newline : str, optional
Defaults to ``
- ``.
- delimiter : str, optional
Defaults to `` ``.
- comment : str or list of str, optional
A pre-header text at the top of the file. This comment is automatically prepended a
#
at the start of new lines, for lists each item corresponds to a newline which is automatically appended.- header : str or list of str, optional
A header for the data (this will be prepended a comment if not present).
- footer : str or list of str, optional
A footer written at the end of the file.
Examples
>>> tbl = tableSile('test.dat', 'w') >>> tbl.write_data(range(2), range(1, 3), comment='A comment', header=['index', 'value']) >>> print(''.join(open('test.dat').readlines())) # A comment #index value 0.00000e+00 1.00000e+00 1.00000e+00 2.00000e+00
-