PageRenderTime 29ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/docs/io/ascii/index.rst

https://gitlab.com/Rockyspade/astropy
ReStructuredText | 264 lines | 201 code | 63 blank | 0 comment | 0 complexity | 31228809381705cc8486f865f9211669 MD5 | raw file
  1. .. include:: references.txt
  2. .. _io-ascii:
  3. *********************************
  4. ASCII Tables (`astropy.io.ascii`)
  5. *********************************
  6. Introduction
  7. ============
  8. `astropy.io.ascii` provides methods for reading and writing a wide range of ASCII data table
  9. formats via built-in :ref:`extension_reader_classes`. The emphasis is on flexibility and ease of use,
  10. although readers can optionally use a less flexible C/Cython engine for reading and writing for
  11. improved performance.
  12. The following shows a few of the ASCII formats that are available, while the section on
  13. `Supported formats`_ contains the full list.
  14. * :class:`~astropy.io.ascii.Basic`: basic table with customizable delimiters and header configurations
  15. * :class:`~astropy.io.ascii.Cds`: `CDS format table <http://vizier.u-strasbg.fr/doc/catstd.htx>`_ (also Vizier and ApJ machine readable tables)
  16. * :class:`~astropy.io.ascii.Daophot`: table from the IRAF DAOphot package
  17. * :class:`~astropy.io.ascii.Ecsv`: `Enhanced CSV format <https://github.com/astropy/astropy-APEs/blob/master/APE6.rst>`_
  18. * :class:`~astropy.io.ascii.FixedWidth`: table with fixed-width columns (see also :ref:`fixed_width_gallery`)
  19. * :class:`~astropy.io.ascii.Ipac`: `IPAC format table <http://irsa.ipac.caltech.edu/applications/DDGEN/Doc/ipac_tbl.html>`_
  20. * :class:`~astropy.io.ascii.HTML`: HTML format table contained in a <table> tag
  21. * :class:`~astropy.io.ascii.Latex`: LaTeX table with datavalue in the ``tabular`` environment
  22. * :class:`~astropy.io.ascii.Rdb`: tab-separated values with an extra line after the column definition line
  23. * :class:`~astropy.io.ascii.SExtractor`: `SExtractor format table <http://www.astromatic.net/software/sextractor>`_
  24. The :mod:`astropy.io.ascii` package is built on a modular and extensible class
  25. structure with independent :ref:`base_class_elements` so that new formats can
  26. be easily accommodated.
  27. .. note::
  28. It is also possible to use the functionality from
  29. :mod:`astropy.io.ascii` through a higher-level interface in the
  30. :ref:`Data Tables <astropy-table>` package. See :ref:`table_io` for more details.
  31. Getting Started
  32. ===============
  33. Reading Tables
  34. --------------
  35. The majority of commonly encountered ASCII tables can be easily read with the |read|
  36. function. Assume you have a file named ``sources.dat`` with the following contents::
  37. obsid redshift X Y object
  38. 3102 0.32 4167 4085 Q1250+568-A
  39. 877 0.22 4378 3892 "Source 82"
  40. This table can be read with the following::
  41. >>> from astropy.io import ascii
  42. >>> data = ascii.read("sources.dat") # doctest: +SKIP
  43. >>> print data # doctest: +SKIP
  44. obsid redshift X Y object
  45. ----- -------- ---- ---- -----------
  46. 3102 0.32 4167 4085 Q1250+568-A
  47. 877 0.22 4378 3892 Source 82
  48. The first argument to the |read| function can be the name of a file, a string
  49. representation of a table, or a list of table lines. The return value
  50. (``data`` in this case) is a :ref:`Table <astropy-table>` object.
  51. By default |read| will try to `guess the table format <#guess-table-format>`_
  52. by trying all the `supported formats`_. If this does not work (for unusually
  53. formatted tables) then one needs give ``astropy.io.ascii`` additional hints about
  54. the format, for example::
  55. >>> lines = ['objID & osrcid & xsrcid ',
  56. ... '----------------------- & ----------------- & -------------',
  57. ... ' 277955213 & S000.7044P00.7513 & XS04861B6_005',
  58. ... ' 889974380 & S002.9051P14.7003 & XS03957B7_004']
  59. >>> data = ascii.read(lines, data_start=2, delimiter='&')
  60. >>> print(data)
  61. objID osrcid xsrcid
  62. --------- ----------------- -------------
  63. 277955213 S000.7044P00.7513 XS04861B6_005
  64. 889974380 S002.9051P14.7003 XS03957B7_004
  65. If the format of a file is known (e.g. it is a fixed width table or an IPAC table),
  66. then it is more efficient and reliable to provide a value for the ``format`` argument from one
  67. of the values in the `supported formats`_. For example::
  68. >>> data = ascii.read(lines, format='fixed_width_two_line', delimiter='&')
  69. For simpler formats such as CSV, |read| will automatically try reading with the
  70. Cython/C parsing engine, which is significantly faster than the ordinary Python
  71. implementation (described in :ref:`fast_ascii_io`). If the fast engine fails,
  72. |read| will fall back on the Python reader by default. The argument
  73. ``fast_reader`` can be specified to control this behavior. For example, to
  74. disable the fast engine::
  75. >>> data = ascii.read(lines, format='csv', fast_reader=False)
  76. Writing Tables
  77. --------------
  78. The |write| function provides a way to write a data table as a formatted ASCII
  79. table. For example the following writes a table as a simple space-delimited
  80. file::
  81. >>> import numpy as np
  82. >>> from astropy.table import Table, Column
  83. >>> x = np.array([1, 2, 3])
  84. >>> y = x ** 2
  85. >>> data = Table([x, y], names=['x', 'y'])
  86. >>> ascii.write(data, 'values.dat')
  87. The ``values.dat`` file will then contain::
  88. x y
  89. 1 1
  90. 2 4
  91. 3 9
  92. Most of the input Reader formats supported by `astropy.io.ascii` for reading are
  93. also supported for writing. This provides a great deal of flexibility in the
  94. format for writing. The example below writes the data as a LaTeX table, using
  95. the option to send the output to ``sys.stdout`` instead of a file::
  96. >>> import sys
  97. >>> ascii.write(data, sys.stdout, format='latex')
  98. \begin{table}
  99. \begin{tabular}{cc}
  100. x & y \\
  101. 1 & 1 \\
  102. 2 & 4 \\
  103. 3 & 9 \\
  104. \end{tabular}
  105. \end{table}
  106. There is also a faster Cython engine for writing simple formats,
  107. which is enabled by default for these formats (see :ref:`fast_ascii_io`).
  108. To disable this engine, use the parameter ``fast_writer``::
  109. >>> ascii.write(data, 'values.csv', format='csv', fast_writer=False) # doctest: +SKIP
  110. Finally, one can write data in the `ECSV table format
  111. <https://github.com/astropy/astropy-APEs/blob/master/APE6.rst>`_ which allows
  112. preserving table meta-data such as column data types and units. In this way a
  113. data table can be stored and read back as ASCII with no loss of information.
  114. >>> t = Table()
  115. >>> t['x'] = Column([1.0, 2.0], unit='m', dtype='float32')
  116. >>> t['y'] = Column([False, True], dtype='bool')
  117. >>> from astropy.extern.six.moves import StringIO
  118. >>> fh = StringIO()
  119. >>> t.write(fh, format='ascii.ecsv') # doctest: +SKIP
  120. >>> table_string = fh.getvalue() # doctest: +SKIP
  121. >>> print(table_string) # doctest: +SKIP
  122. # %ECSV 0.9
  123. # ---
  124. # columns:
  125. # - {name: x, unit: m, type: float32}
  126. # - {name: y, type: bool}
  127. x y
  128. 1.0 False
  129. 2.0 True
  130. >>> Table.read(table_string, format='ascii') # doctest: +SKIP
  131. <Table masked=False length=2>
  132. x y
  133. m
  134. float32 bool
  135. ------- -----
  136. 1.0 False
  137. 2.0 True
  138. .. _supported_formats:
  139. Supported formats
  140. =================
  141. A full list of the supported ``format`` values and corresponding format types for ASCII
  142. tables is given below. The ``Write`` column indicates which formats support write
  143. functionality, and the ``Fast`` column indicates which formats are compatible with
  144. the fast Cython/C engine for reading and writing.
  145. ========================= ===== ==== ============================================================================================
  146. Format Write Fast Description
  147. ========================= ===== ==== ============================================================================================
  148. ``aastex`` Yes :class:`~astropy.io.ascii.AASTex`: AASTeX deluxetable used for AAS journals
  149. ``basic`` Yes Yes :class:`~astropy.io.ascii.Basic`: Basic table with custom delimiters
  150. ``cds`` :class:`~astropy.io.ascii.Cds`: CDS format table
  151. ``commented_header`` Yes Yes :class:`~astropy.io.ascii.CommentedHeader`: Column names in a commented line
  152. ``csv`` Yes Yes :class:`~astropy.io.ascii.Csv`: Basic table with comma-separated values
  153. ``daophot`` :class:`~astropy.io.ascii.Daophot`: IRAF DAOphot format table
  154. ``ecsv`` Yes :class:`~astropy.io.ascii.Ecsv`: Enhanced CSV format
  155. ``fixed_width`` Yes :class:`~astropy.io.ascii.FixedWidth`: Fixed width
  156. ``fixed_width_no_header`` Yes :class:`~astropy.io.ascii.FixedWidthNoHeader`: Fixed width with no header
  157. ``fixed_width_two_line`` Yes :class:`~astropy.io.ascii.FixedWidthTwoLine`: Fixed width with second header line
  158. ``html`` Yes :class:`~astropy.io.ascii.HTML`: HTML format table
  159. ``ipac`` Yes :class:`~astropy.io.ascii.Ipac`: IPAC format table
  160. ``latex`` Yes :class:`~astropy.io.ascii.Latex`: LaTeX table
  161. ``no_header`` Yes Yes :class:`~astropy.io.ascii.NoHeader`: Basic table with no headers
  162. ``rdb`` Yes Yes :class:`~astropy.io.ascii.Rdb`: Tab-separated with a type definition header line
  163. ``sextractor`` :class:`~astropy.io.ascii.SExtractor`: SExtractor format table
  164. ``tab`` Yes Yes :class:`~astropy.io.ascii.Tab`: Basic table with tab-separated values
  165. ========================= ===== ==== ============================================================================================
  166. Using `astropy.io.ascii`
  167. ========================
  168. The details of using `astropy.io.ascii` are provided in the following sections:
  169. Reading tables
  170. ---------------
  171. .. toctree::
  172. :maxdepth: 2
  173. read
  174. Writing tables
  175. ---------------
  176. .. toctree::
  177. :maxdepth: 2
  178. write
  179. Fixed-width Gallery
  180. --------------------
  181. .. toctree::
  182. :maxdepth: 2
  183. fixed_width_gallery
  184. Fast ASCII Engine
  185. -----------------
  186. .. toctree::
  187. :maxdepth: 2
  188. fast_ascii_io
  189. Base class elements
  190. -------------------
  191. .. toctree::
  192. :maxdepth: 2
  193. base_classes
  194. Extension Reader classes
  195. ------------------------
  196. .. toctree::
  197. :maxdepth: 2
  198. extension_classes
  199. Reference/API
  200. =============
  201. .. automodapi:: astropy.io.ascii