read_data

sherpa.io.read_data(filename: str, ncols: int = 2, colkeys: 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:
filenamestr

The name of the ASCII file to read in.

ncolsint, optional

The number of columns to read in (the first ncols columns in the file). This is ignored if colkeys is given.

colkeysarray of str, optional

An array of the column name to read in. The default is None.

sepstr, optional

The separator character. The default is ' '.

dstypedata class to use, optional

The class of the data object to create.

commentstr, optional

The comment character. The default is '#'.

require_floatsbool, optional

If True (the default), non-numeric data values will raise a ValueError.

Returns:
data

The data object (created by calling the dstype constructor with the filename and then the data columns from the file).

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.

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'])