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())) # doctest: +NORMALIZE_WHITESPACE
0.00000e+00 2.00000e+00     4.00000e+00
1.00000e+00 3.00000e+00     5.00000e+00
<BLANKLINE>
>>> a = np.arange(6).reshape(3, 2)
>>> tbl = tableSile('test.dat', 'w')
>>> tbl.write_data(a, comment='Hello')
>>> print(''.join(open('test.dat').readlines())) # doctest: +NORMALIZE_WHITESPACE
# Hello
0.00000e+00 2.00000e+00     4.00000e+00
1.00000e+00 3.00000e+00     5.00000e+00
<BLANKLINE>
>>> 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())) # doctest: +NORMALIZE_WHITESPACE
#x  y
0.00000e+00 2.00000e+00     4.00000e+00
1.00000e+00 3.00000e+00     5.00000e+00
<BLANKLINE>
>>> a = np.arange(6).reshape(3, 2)
>>> tbl = tableSile('test.dat', 'w')
>>> tbl.write_data(a.T)
>>> print(''.join(open('test.dat').readlines())) # doctest: +NORMALIZE_WHITESPACE
0.00000e+00 1.00000e+00
2.00000e+00 3.00000e+00
4.00000e+00 5.00000e+00
<BLANKLINE>
>>> 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())) # doctest: +NORMALIZE_WHITESPACE
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
<BLANKLINE>

Attributes

base_file

File of the current Sile

file

File of the current Sile

Methods

__init__(self, filename[, mode, comment])

Initialize self.

dir_file(self[, filename])

File of the current Sile

exist(self)

Query whether the file exists

read(self, \*args, \*\*kwargs)

Generic read method which should be overloaded in child-classes

read_data(self, \*args, \*\*kwargs)

Read tabular data from the file.

write(self, \*args, \*\*kwargs)

Generic write method which should be overloaded in child-classes

write_data(self, \*args, \*\*kwargs)

Write tabular data to the file with optional header.

property base_file

File of the current Sile

dir_file(self, filename=None)

File of the current Sile

exist(self)

Query whether the file exists

property file

File of the current Sile

read(self, *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(self, *args, **kwargs)[source]

Read tabular data from the file.

Parameters
columnslist of int, optional

only return the indices of the columns that are provided

delimiterstr, optional

the delimiter used in the file, will automatically try to guess if not specified

ret_commentbool, optional

also return the comments at the top of the file (if queried)

ret_headerbool, optional

also return the header information (if queried)

commentstr, optional

lines starting with this are discarded as comments

write(self, *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(self, *args, **kwargs)[source]

Write tabular data to the file with optional header.

Parameters
*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.

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())) # doctest: +NORMALIZE_WHITESPACE
# A comment
#index    value
0.00000e+00 1.00000e+00
1.00000e+00 2.00000e+00
<BLANKLINE>