read_data, ncols=2, colkeys=None, sep=' ', dstype=<class ''>, comment='#', require_floats=True)[source] [edit on github]

Create a data object from an ASCII file.

  • 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 if colkeys 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 a ValueError.


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

Return type


  • 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.


The file format is described in get_ascii_data.


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,

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