tableSile¶
-
class
sisl.io.table.
tableSile
(filename, mode='r', comment=None, *args, **kwargs)¶ Bases:
sisl.io.Sile
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
__dict__
__doc__
__module__
__weakref__
list of weak references to the object (if defined)
_write_default_only
File of the current
Sile
File of the current
Sile
Methods
_ArgumentParser_args_single
()Default arguments for the Sile
__delattr__
Implement delattr(self, name).
__dir__
Default dir() implementation.
__enter__
()Opens the output file and returns it self
__eq__
Return self==value.
__exit__
(type, value, traceback)__format__
Default object formatter.
__ge__
Return self>=value.
__getattr__
(name)Override to check the handle
__getattribute__
Return getattr(self, name).
__gt__
Return self>value.
__hash__
Return hash(self).
__init__
(filename[, mode, comment])Initialize self.
__init_subclass__
This method is called when a class is subclassed.
__iter__
()Iterator for file
__le__
Return self<=value.
__lt__
Return self<value.
__ne__
Return self!=value.
__new__
Create and return a new object.
__reduce__
Helper for pickle.
__reduce_ex__
Helper for pickle.
__repr__
Return repr(self).
__setattr__
Implement setattr(self, name, value).
__sizeof__
Size of object in memory, in bytes.
__str__
()Return a representation of the
Sile
__subclasshook__
Abstract classes can override this to customize issubclass().
_base_file
(f)Make f refer to the file with the appropriate base directory
_base_setup
(*args, **kwargs)Setup the
Sile
after initialization_open
()_setup
(*args, **kwargs)Setup the
tableSile
after initialization_write
(*args, **kwargs)Wrapper to default the write statements
_write_default
(*args, **kwargs)Write tabular data to the file with optional header.
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.
-
property
base_file
¶ File of the current
Sile
-
dir_file
(filename=None)¶ File of the current
Sile
-
exist
()¶ Query whether the file exists
-
property
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.
- *argsarray_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).- fmtstr, optional
The formatting string, defaults to
.5e
.- fmtsstr, optional
The formatting string (for all columns), defaults to
fmt * len(args)
. fmts has precedence over fmt.- newlinestr, optional
Defaults to ``
- ``.
- delimiterstr, optional
Defaults to `` ``.
- commentstr 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.- headerstr or list of str, optional
A header for the data (this will be prepended a comment if not present).
- footerstr or list of str, optional
A footer written at the end of the file.
>>> 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
-
property