An extensible ASCII table reader and writer.
Read the input table. If numpy is True (default) return the table in a numpy record array. Otherwise return the table as a dictionary of column objects using plain python lists to hold the data. Most of the default behavior for various parameters is determined by the Reader class.
| Parameters: |
|
|---|
Initialize a table reader allowing for common customizations. Most of the default behavior for various parameters is determined by the Reader class.
| Parameters: |
|
|---|
Write the input table to filename. Most of the default behavior for various parameters is determined by the Writer class.
| Parameters: |
|
|---|
Initialize a table writer allowing for common customizations. Most of the default behavior for various parameters is determined by the Writer class.
| Parameters: |
|
|---|
Return a tuple (converter_func, converter_type). The converter function converts a list into a list of the given python_type. This argument is a function that takes a single argument and returns a single value of the desired type. In general this will be one of int, float or str. The converter type is used to track the generic data type (int, float, str) that is produced by the converter function.
Return a tuple (converter_func, converter_type). The converter function converts a list into a numpy array of the given numpy_type. This type must be a valid numpy type, e.g. numpy.int, numpy.uint, numpy.int8, numpy.int64, numpy.float, numpy.float64, numpy.str. The converter type is used to track the generic data type (int, float, str) that is produced by the converter function.
Set the default value of the guess parameter for read()
| Parameters: | guess – New default guess value (True|False) |
|---|
Bases: object
Class providing methods to read an ASCII table using the specified header, data, inputter, and outputter instances.
Typical usage is to instantiate a Reader() object and customize the header, data, inputter, and outputter attributes. Each of these is an object of the corresponding class.
There is one method inconsistent_handler that can be used to customize the behavior of read() in the event that a data row doesn’t match the header. The default behavior is to raise an InconsistentTableError.
Return lines in the table that match header.comment regexp
Adjust or skip data entries if a row is inconsistent with the header.
The default implementation does no adjustment, and hence will always trigger an exception in read() any time the number of data entries does not match the header.
Note that this will not be called if the row already matches the header.
| Parameters: |
|
|---|---|
| Returns: | list of strings to be parsed into data entries in the output table. If the length of this list does not match ncols, an exception will be raised in read(). Can also be None, in which case the row will be skipped. |
Read the table and return the results in a format determined by the outputter attribute.
The table parameter is any string or object that can be processed by the instance inputter. For the base Inputter class table can be one of:
| Parameters: | table – table input |
|---|---|
| Returns: | output table |
Write table as list of strings.
| Parameters: | table – asciitable Reader object |
|---|---|
| Returns: | list of strings corresponding to ASCII table |
Bases: object
Base table data reader.
| Parameters: |
|
|---|
alias of str
Set the data_lines attribute to the lines slice comprising the table data values.
Return a generator that returns a list of column values (as strings) for each data line.
Set fill value for each column and then apply that fill value
In the first step it is evaluated with value from fill_values applies to which column using fill_include_names and fill_exclude_names. In the second step all replacements are done for the appropriate columns.
Strip out comment lines and blank lines from list of lines
| Parameters: | lines – all lines in table |
|---|---|
| Returns: | list of lines |
alias of DefaultSplitter
Bases: object
Base table header reader
| Parameters: |
|
|---|
Return the column names of the table
Initialize the header Column objects from the table lines.
Based on the previously set Header attributes find or create the column names. Sets self.cols with the list of Columns. This list only includes the actual requested columns after filtering by the include_names and exclude_names attributes. See self.names for the full list.
| Parameters: | lines – list of table lines |
|---|---|
| Returns: | None |
Return the number of expected data columns from data splitting. This is either explicitly set (typically for fixedwidth splitters) or set to self.names otherwise.
Generator to yield non-comment lines
alias of DefaultSplitter
Bases: object
Get the lines from the table input and return a list of lines. The input table can be one of:
Get the lines from the table input.
| Parameters: | table – table input |
|---|---|
| Returns: | list of lines |
Process lines for subsequent use. In the default case do nothing. This routine is not generally intended for removing comment lines or stripping whitespace. These are done (if needed) in the header and data line processing.
Override this method if something more has to be done to convert raw input lines to the table rows. For example the ContinuationLinesInputter derived class accounts for continuation characters if a row is split into lines.
Bases: object
Output table as a dict of column objects keyed on column name. The table data are stored as plain python lists within the column objects.
Bases: object
Base splitter that uses python’s split method to do the work.
This does not handle quoted values. A key feature is the formulation of __call__ as a generator that returns a list of the split line values at each iteration.
There are two methods that are intended to be overridden, first process_line() to do pre-processing on each input line before splitting and process_val() to do post-processing on each split string value. By default these apply the string strip() function. These can be set to another function via the instance attribute or be disabled entirely, for example:
reader.header.splitter.process_val = lambda x: x.lstrip()
reader.data.splitter.process_val = None
| Parameters: | delimiter – one-character string used to separate fields |
|---|
Remove whitespace at the beginning or end of line. This is especially useful for whitespace-delimited files to prevent spurious columns at the beginning or end.
Remove whitespace at the beginning or end of value.
Bases: object
Table column.
The key attributes of a Column object are:
Bases: astropy.io.ascii.core.BaseSplitter
Default class to split strings into columns using python csv. The class attributes are taken from the csv Dialect class.
Typical usage:
# lines = ..
splitter = asciitable.DefaultSplitter()
for col_vals in splitter(lines):
for col_val in col_vals:
...
| Parameters: |
|
|---|
Remove whitespace at the beginning or end of line. This is especially useful for whitespace-delimited files to prevent spurious columns at the beginning or end. If splitting on whitespace then replace unquoted tabs with space first
Remove whitespace at the beginning or end of value.
Bases: dict
Provide minimal compatibility with numpy rec array API for BaseOutputter object:
table = asciitable.read('mytable.dat', numpy=False)
table.field('x') # List of elements in column 'x'
table.dtype.names # get column names in order
table[1] # returns row 1 as a list
table[1][2] # 3nd column in row 1
table['col1'][1] # Row 1 in column col1
for row_vals in table: # iterate over table rows
print row_vals # print list of vals in each row
Bases: object
Bases: exceptions.ValueError
Bases: astropy.io.ascii.core.BaseOutputter
Output the table as a numpy.rec.recarray
Missing or bad data values are handled at two levels. The first is in the data reading step where if data.fill_values is set then any occurences of a bad value are replaced by the correspond fill value. At the same time a boolean list mask is created in the column object.
The second stage is when converting to numpy arrays which by default generates masked arrays, if data.fill_values is set and plain arrays if it is not. In the rare case that plain arrays are needed set auto_masked (default = True) and default_masked (default = False) to control this behavior as follows:
| auto_masked | default_masked | fill_values | output |
|---|---|---|---|
| – | True | – | masked_array |
| – | False | None | array |
| True | – | dict(..) | masked_array |
| False | – | dict(..) | array |
To set these values use:
Outputter = asciitable.NumpyOutputter()
Outputter.default_masked = True
The following classes extend the base Reader functionality to handle different table formats. Some, such as the Basic Reader class are fairly general and include a number of configurable attributes. Others such as Cds or Daophot are specialized to read certain well-defined but idiosyncratic formats.
Bases: astropy.io.ascii.latex.Latex
Write and read AASTeX tables.
This class implements some AASTeX specific commands. AASTeX is used for the AAS (American Astronomical Society) publications like ApJ, ApJL and AJ.
It derives from Latex and accepts the same keywords (see Latex for documentation). However, the keywords header_start, header_end, data_start and data_end in latexdict have no effect.
Bases: astropy.io.ascii.core.BaseReader
Read a character-delimited table with a single header line at the top followed by data lines to the end of the table. Lines beginning with # as the first non-whitespace character are comments. This reader is highly configurable.
rdr = asciitable.get_reader(Reader=asciitable.Basic)
rdr.header.splitter.delimiter = ' '
rdr.data.splitter.delimiter = ' '
rdr.header.start_line = 0
rdr.data.start_line = 1
rdr.data.end_line = None
rdr.header.comment = r'\s*#'
rdr.data.comment = r'\s*#'
Example table:
# Column definition is the first uncommented line
# Default delimiter is the space character.
apples oranges pears
# Data starts after the header column definition, blank lines ignored
1 2 3
4 5 6
Bases: astropy.io.ascii.core.BaseReader
Read a CDS format table: http://vizier.u-strasbg.fr/doc/catstd.htx. Example:
.. py:method:: Cds.write(table=None)
module: astropy.io.ascii Not available for the Cds class (raises NotImplementedError)
Bases: astropy.io.ascii.core.BaseReader
Read a file where the column names are given in a line that begins with the header comment character. The default delimiter is the <space> character.:
# col1 col2 col3
# Comment line
1 2 3
4 5 6
Bases: astropy.io.ascii.core.BaseReader
Read a DAOphot file. Example:
#K MERGERAD = INDEF scaleunit %-23.7g
#K IRAF = NOAO/IRAFV2.10EXPORT version %-23s
#K USER = davis name %-23s
#K HOST = tucana computer %-23s
#
#N ID XCENTER YCENTER MAG MERR MSKY NITER \
#U ## pixels pixels magnitudes magnitudes counts ## \
#F %-9d %-10.3f %-10.3f %-12.3f %-14.3f %-15.7g %-6d
#
#N SHARPNESS CHI PIER PERROR \
#U ## ## ## perrors \
#F %-23.3f %-12.3f %-6d %-13s
#
14 138.538 256.405 15.461 0.003 34.85955 4 \
-0.032 0.802 0 No_error
The keywords defined in the #K records are available via the Daophot reader object:
reader = asciitable.get_reader(Reader=asciitable.DaophotReader)
data = reader.read('t/daophot.dat')
for keyword in reader.keywords:
print keyword.name, keyword.value, keyword.units, keyword.format
Bases: astropy.io.ascii.core.BaseReader
Read or write a fixed width table with a single header line that defines column names and positions. Examples:
# Bar delimiter in header and data
| Col1 | Col2 | Col3 |
| 1.2 | hello there | 3 |
| 2.4 | many words | 7 |
# Bar delimiter in header only
Col1 | Col2 | Col3
1.2 hello there 3
2.4 many words 7
# No delimiter with column positions specified as input
Col1 Col2Col3
1.2hello there 3
2.4many words 7
See the Fixed-width Gallery for specific usage examples.
| Parameters: |
|
|---|
Bases: astropy.io.ascii.fixedwidth.FixedWidth
Read or write a fixed width table which has no header line. Column names are either input (names keyword) or auto-generated. Column positions are determined either by input (col_starts and col_stops keywords) or by splitting the first data line. In the latter case a delimiter is required to split the data line.
Examples:
# Bar delimiter in header and data
| 1.2 | hello there | 3 |
| 2.4 | many words | 7 |
# Compact table having no delimiter and column positions specified as input
1.2hello there3
2.4many words 7
This class is just a convenience wrapper around FixedWidth but with header.start_line = None and data.start_line = 0.
See the Fixed-width Gallery for specific usage examples.
| Parameters: |
|
|---|
Bases: astropy.io.ascii.fixedwidth.FixedWidth
Read or write a fixed width table which has two header lines. The first header line defines the column names and the second implicitly defines the column positions. Examples:
# Typical case with column extent defined by ---- under column names.
col1 col2 <== header_start = 0
----- ------------ <== position_line = 1, position_char = "-"
1 bee flies <== data_start = 2
2 fish swims
# Pretty-printed table
+------+------------+
| Col1 | Col2 |
+------+------------+
| 1.2 | "hello" |
| 2.4 | there world|
+------+------------+
See the Fixed-width Gallery for specific usage examples.
| Parameters: |
|
|---|
Bases: astropy.io.ascii.core.BaseReader
Read an IPAC format table: http://irsa.ipac.caltech.edu/applications/DDGEN/Doc/ipac_tbl.html:
\name=value
\ Comment
| column1 | column2 | column3 | column4 | column5 |
| double | double | int | double | char |
| unit | unit | unit | unit | unit |
| null | null | null | null | null |
2.0978 29.09056 73765 2.06000 B8IVpMnHg
Or:
|-----ra---|----dec---|---sao---|------v---|----sptype--------|
2.09708 29.09056 73765 2.06000 B8IVpMnHg
Caveats:
Overcoming these limitations would not be difficult, code contributions welcome from motivated users.
Not available for the Ipac class (raises NotImplementedError)
Bases: astropy.io.ascii.core.BaseReader
Write and read LaTeX tables.
This class implements some LaTeX specific commands. Its main purpose is to write out a table in a form that LaTeX can compile. It is beyond the scope of this class to implement every possible LaTeX command, instead the focus is to generate a syntactically valid LaTeX tables. This class can also read simple LaTeX tables (one line per table row, no \multicolumn or similar constructs), specifically, it can read the tables that it writes.
Reading a LaTeX table, the following keywords are accepted:
When writing a LaTeX table, the some keywords can customize the format. Care has to be taken here, because python interprets \ in a string as an escape character. In order to pass this to the output either format your strings as raw strings with the r specifier or use a double \\. Examples:
caption = r'My table \label{mytable}'
caption = 'My table \\label{mytable}'
The default is \begin{table}. The following would generate a table, which spans the whole page in a two-column document:
asciitable.write(data, sys.stdout, Writer = asciitable.Latex,
latexdict = {'tabletype': 'table*'})
If not present all columns will be centered.
This will appear above the table as it is the standard in many scientific publications. If you prefer a caption below the table, just write the full LaTeX command as latexdict['tablefoot'] = r'\caption{My table}'
Each one can be a string or a list of strings. These strings will be inserted into the table without any further processing. See the examples below.
Keys in this dictionary should be names of columns. If present, a line in the LaTeX table directly below the column names is added, which contains the values of the dictionary. Example:
import asciitable
import asciitable.latex
import sys
data = {'name': ['bike', 'car'], 'mass': [75,1200], 'speed': [10, 130]}
asciitable.write(data, sys.stdout, Writer = asciitable.Latex,
latexdict = {'units': {'mass': 'kg', 'speed': 'km/h'}})
If the column has no entry in the units dictionary, it defaults to ' '.
Run the following code to see where each element of the dictionary is inserted in the LaTeX table:
import asciitable
import asciitable.latex
import sys
data = {'cola': [1,2], 'colb': [3,4]}
asciitable.write(data, sys.stdout, Writer = asciitable.Latex,
latexdict = asciitable.latex.latexdicts['template'])
Some table styles are predefined in the dictionary asciitable.latex.latexdicts. The following generates in table in style preferred by A&A and some other journals:
asciitable.write(data, sys.stdout, Writer = asciitable.Latex,
latexdict = asciitable.latex.latexdicts['AA'])
As an example, this generates a table, which spans all columns and is centered on the page:
asciitable.write(data, sys.stdout, Writer = asciitable.Latex,
col_align = '|lr|',
latexdict = {'preamble': r'egin{center}', 'tablefoot': r'\end{center}',
'tabletype': 'table*'})
Shorthand for:
latexdict['caption'] = caption
If not present this will be auto-generated for centered columns. Shorthand for:
latexdict['col_align'] = col_align
Bases: astropy.io.ascii.core.BaseReader
Read a table from a data object in memory. Several input data formats are supported:
Output of asciitable.read():
table = asciitable.get_reader(Reader=asciitable.Daophot)
data = table.read('t/daophot.dat')
mem_data_from_table = asciitable.read(table, Reader=asciitable.Memory)
mem_data_from_data = asciitable.read(data, Reader=asciitable.Memory)
Numpy structured array:
data = numpy.zeros((2,), dtype=[('col1','i4'), ('col2','f4'), ('col3', 'a10')])
data[:] = [(1, 2., 'Hello'), (2, 3., "World")]
mem_data = asciitable.read(data, Reader=asciitable.Memory)
Numpy masked structured array:
data = numpy.ma.zeros((2,), dtype=[('col1','i4'), ('col2','f4'), ('col3', 'a10')])
data[:] = [(1, 2., 'Hello'), (2, 3., "World")]
data['col2'] = ma.masked
mem_data = asciitable.read(data, Reader=asciitable.Memory)
In the current version all masked values will be converted to nan.
Sequence of sequences:
data = [[1, 2, 3 ],
[4, 5.2, 6.1 ],
[8, 9, 'hello']]
mem_data = asciitable.read(data, Reader=asciitable.Memory, names=('c1','c2','c3'))
Dict of sequences:
data = {'c1': [1, 2, 3],
'c2': [4, 5.2, 6.1],
'c3': [8, 9, 'hello']}
mem_data = asciitable.read(data, Reader=asciitable.Memory, names=('c1','c2','c3'))
Not available for the Memory class (raises NotImplementedError)
Bases: astropy.io.ascii.basic.Basic
Read a table with no header line. Columns are autonamed using header.auto_format which defaults to “col%d”. Otherwise this reader the same as the Basic class from which it is derived. Example:
# Table data
1 2 "hello there"
3 4 world
Bases: astropy.io.ascii.basic.Tab
Read a tab-separated file with an extra line after the column definition line. The RDB format meets this definition. Example:
col1 <tab> col2 <tab> col3
N <tab> S <tab> N
1 <tab> 2 <tab> 5
In this reader the second line is just ignored.
Bases: astropy.io.ascii.basic.Basic
Read a tab-separated file. Unlike the Basic reader, whitespace is not stripped from the beginning and end of lines. By default whitespace is still stripped from the beginning and end of individual column values.
Example:
col1 <tab> col2 <tab> col3
# Comment line
1 <tab> 2 <tab> 5
These classes provide support for extension readers.
Bases: astropy.io.ascii.core.BaseData
CDS table data reader
alias of FixedWidthSplitter
Bases: astropy.io.ascii.core.BaseHeader
Bases: astropy.io.ascii.core.BaseHeader
Header class for which the column definition line starts with the comment character. See the CommentedHeader class for an example.
Bases: astropy.io.ascii.core.BaseInputter
Inputter where lines ending in continuation_char are joined with the subsequent line. Example:
col1 col2 col3
1 2 3
4 5 6
Bases: astropy.io.ascii.core.BaseHeader
Read the header from a file produced by the IRAF DAOphot routine.
Bases: astropy.io.ascii.core.BaseSplitter
Split line based on fixed start and end positions for each col in self.cols.
This class requires that the Header class will have defined col.start and col.end for each column. The reference to the header.cols gets put in the splitter object by the base Reader.read() function just in time for splitting data lines by a data object.
Note that the start and end positions are defined in the pythonic style so line[start:end] is the desired substring for a column. This splitter class does not have a hook for process_lines since that is generally not useful for fixed-width input.
Bases: astropy.io.ascii.core.BaseHeader
Fixed width table header reader.
The key settable class attributes are:
| Parameters: |
|
|---|
Initialize the header Column objects from the table lines.
Based on the previously set Header attributes find or create the column names. Sets self.cols with the list of Columns. This list only includes the actual requested columns after filtering by the include_names and exclude_names attributes. See self.names for the full list.
| Parameters: | lines – list of table lines |
|---|---|
| Returns: | None |
Split line on the delimiter and determine column values and column start and end positions. This might include null columns with zero length (e.g. for header row = "| col1 || col2 | col3 |" or header2_row = "----- ------- -----"). The null columns are stripped out. Returns the values between delimiters and the corresponding start and end positions.
| Parameters: | line – input line |
|---|---|
| Returns: | (vals, starts, ends) |
Bases: astropy.io.ascii.core.BaseData
Base table data reader.
| Parameters: |
|
|---|
alias of FixedWidthSplitter
Bases: astropy.io.ascii.core.BaseData
IPAC table data reader
alias of FixedWidthSplitter
Bases: astropy.io.ascii.core.BaseHeader
IPAC table header
Initialize the header Column objects from the table lines.
Based on the previously set Header attributes find or create the column names. Sets self.cols with the list of Columns. This list only includes the actual requested columns after filtering by the include_names and exclude_names attributes. See self.names for the full list.
| Parameters: | lines – list of table lines |
|---|---|
| Returns: | list of table Columns |
Generator to yield IPAC header lines, i.e. those starting and ending with delimiter character.
alias of BaseSplitter
Bases: astropy.io.ascii.core.BaseHeader
Bases: astropy.io.ascii.core.BaseData
Bases: astropy.io.ascii.core.BaseSplitter
Split LaTeX table date. Default delimiter is &.
Bases: astropy.io.ascii.latex.LatexHeader
In a deluxetable some header keywords differ from standard LaTeX.
This header is modified to take that into account.
Bases: astropy.io.ascii.latex.LatexData
In a deluxetable the data is enclosed in startdata and enddata
Bases: astropy.io.ascii.latex.LatexSplitter
extract column names from a deluxetable
This splitter expects the following LaTeX code in a single line:
ablehead{colhead{col1} & ... & colhead{coln}}