read_data
- sherpa.io.read_data(filename: str, ncols: int = 2, colkeys: ~typing.Sequence[str] | None = None, sep: str = ' ', dstype=<class 'sherpa.data.Data1D'>, comment: str = '#', require_floats: bool = True) Data [source] [edit on github]
Create a data object from an ASCII file.
- Parameters:
filename (str) – The name of the ASCII file to read in.
ncols (int, optional) – The number of columns to read in (the first
ncols
columns in the file). This is ignored ifcolkeys
is given.colkeys (array of str, optional) – An array of the column name to read in. The default is
None
.sep (str, optional) – The separator character. The default is
' '
.dstype (data class to use, optional) – The class of the data object to create.
comment (str, optional) – The comment character. The default is
'#'
.require_floats (bool, optional) – If
True
(the default), non-numeric data values will raise aValueError
.
- Returns:
The data object (created by calling the dstype constructor with the filename and then the data columns from the file).
- Return type:
data
- Raises:
sherpa.utils.err.IOErr – Raised if a requested column is missing or the file appears to be a binary file.
ValueError – If a column value can not be converted into a numeric value and the
require_floats
parameter is True.
See also
Notes
The file format is described in
get_ascii_data
.Examples
Create a 1D data object from the first two columns in the file:
>>> dat = read_data('src.dat')
Use the third column as the error column (statistical):
>>> dat = read_data('src.dat', ncols=3)
Read in a histogram data set, using the columns XLO, XHI, and Y:
>>> cols = ['XLO', 'XHI', 'Y'] >>> dat = read_data('hist.dat', colkeys=cols, dstype=sherpa.data.Data1DInt)
Use the first and third column from the file cols.dat, where the file has no header information:
>>> dat = read_data('cols.dat', colkeys=['col1', 'col3'])