Reader#

The full flexibility of the astroparse library is available through the Reader class.

class astroparse.Reader(sep_reg: str = '(?<=\\S)( |\\t)+(?!\\s)', nan_reg: str = 'nan', hdr: int = -1, lo: int = 1, hi: int = -1)#

The Reader object is a versatile class that can be leveraged to parse multiple input files of multiple formats.

__init__(sep_reg: str = '(?<=\\S)( |\\t)+(?!\\s)', nan_reg: str = 'nan', hdr: int = -1, lo: int = 1, hi: int = -1)#

Initializes a Reader object with the default values for its parsing.

The values with which a Reader is initialized define the default behavior for parsing files unless overridden in the Reader instance methods.

Parameters:
  • sep_reg (str) – Regex pattern matching column separators in the input file.

  • nan_reg (str) – Regex pattern matching empty or NaN data in the input file.

  • hdr (int, default = -1) – Line number in the input file where the header is found. Only one line can be read as the ‘header’ and it must have the same column format as the data. A value of -1 means no header will be prepended to the data.

  • lo (int, default = 1) – First line number in the input file where the data appear.

  • hi (int, default = -1) – Last line number in the input file where the data appear.

parse_file(fname_in: str, sep_reg: str = None, nan_reg: str = None, hdr: int = None, lo: int = None, hi: int = None, fname_out: str = None) Table#

Translates the contents of a file into string interpretable by astropy readers.

The parsed contents are returned as an astropy Table and can be optionally be saved to an output file. Empty or NaN data can be replaced according to a specified pattern. Unless otherwise specified, the value for all arguments is None and will default to those with which the Reader object was initialized.

Parameters:
  • fname_in (str) – Name of the input file.

  • sep_reg (str, default = None) – Regex pattern matching column separators in the input file.

  • nan_reg (str, default = None) – Regex pattern matching empty or NaN data in the input file.

  • hdr (int, default = None) – Line number in the input file where the header is found. Only one line can be read as the ‘header’ and it must have the same column format as the data. A value of -1 means no header will be prepended to the data.

  • lo (int, default = None) – First line number in the input file where the data appear.

  • hi (int, default = None) – Last line number in the input file where the data appear.

  • fname_out (str, default = None) – Name of the output file. A value of None indicates that data should not be saved to any external file. If a file name is specified, the contents of any file at that existing path will be overwritten.

Returns:

tbl – The parsed contents of the input file as an astropy Table.

Return type:

astropy.table.Table

from_dicts(dict_list: dict[list]) list[Table]#

Parse multiple files using dictionaries of arguments to override the default parsing behavior for this Reader instance.

Each dictionary must have keywords that match the method signature of parse_file. Not all keywords must be specified; only those for which the defaults for the given Reader instance are not sufficient.

Parameters:

dict_list (list[dict]) – List of arguments for each input file to be passed to parse_file.

Returns:

tbl_list – List of the parsed contents of each input file as astropy Table.

Return type:

list[astropy.table.Table]

from_lists(flist: list[str], sep_list: list[str] = None, nan_list: list[str] = None, hdr_list: list[int] = None, lo_list: list[int] = None, hi_list: list[int] = None, out_list: list[str] = None) list[Table]#

Parse multiple files using lists of arguments to override the default behavior for this Reader instance.

Unless otherwise specified, the value for all lists is None and will parse each file using the default behavior for the corresponding argument provided to this reader. All non-None lists must be of the same length but can still contain None to use the default behavior.

Parameters:
  • flist (list[str]) – List of input file names.

  • sep_list (list[str], default = None) – List of separator regex patterns.

  • nan_list (list[str], default = None) – List of NaN regex patterns.

  • hdr_list (list[int], default = None) – List of line numbers where the header can be found in each file.

  • lo_list (list[int], default = None) – List of first lines where data appear in each file.

  • hi_list (list[int], default = None) – List of last lines where data appear in each file.

  • fname_out (list[str], default = None) – List of output file names.

Returns:

tbl_list – List of the parsed contents of each input file as astropy Table.

Return type:

list[astropy.table.Table]