File: fixed_width_gallery.rst

package info (click to toggle)
astropy 7.0.1-3
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 35,328 kB
  • sloc: python: 233,437; ansic: 55,264; javascript: 17,680; lex: 8,621; sh: 3,317; xml: 2,287; makefile: 191
file content (567 lines) | stat: -rw-r--r-- 15,979 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
.. include:: references.txt

.. _fixed_width_gallery:

Fixed-Width Gallery
*******************

Fixed-width tables are those where each column has the same width for every row
in the table. This is commonly used to make tables easy to read for humans or
Fortran codes. It also reduces issues with quoting and special characters,
for example::

  Col1   Col2    Col3 Col4
  ---- --------- ---- ----
   1.2   "hello"    1    a
   2.4 's worlds    2    2

There are a number of common variations in the formatting of fixed-width tables
which :mod:`astropy.io.ascii` can read and write. The most significant
difference is whether there is no header line (:class:`~astropy.io.ascii.FixedWidthNoHeader`), one
header line (:class:`~astropy.io.ascii.FixedWidth`), or two header lines
(:class:`~astropy.io.ascii.FixedWidthTwoLine`). Next, there are variations in
the delimiter character, like whether the delimiter appears on either end
("bookends"), or if there is padding around the delimiter.

Details are available in the class API documentation, but the easiest way to
understand all of the options and their interactions is by example.

Reading
=======

..
  EXAMPLE START
  Reading Fixed-Width Tables

Fixed Width
-----------

**Nice, typical, fixed-format table:**
::

  >>> from astropy.io import ascii
  >>> table = """
  ... # comment (with blank line above)
  ... |  Col1  |  Col2   |
  ... |  1.2   | "hello" |
  ... |  2.4   |'s worlds|
  ... """
  >>> ascii.read(table, format='fixed_width')
  <Table length=2>
    Col1     Col2
  float64    str9
  ------- ---------
      1.2   "hello"
      2.4 's worlds

**Typical fixed-format table with col names provided:**
::

  >>> table = """
  ... # comment (with blank line above)
  ... |  Col1  |  Col2   |
  ... |  1.2   | "hello" |
  ... |  2.4   |'s worlds|
  ... """
  >>> ascii.read(table, format='fixed_width', names=['name1', 'name2'])
  <Table length=2>
   name1    name2
  float64    str9
  ------- ---------
      1.2   "hello"
      2.4 's worlds

**Weird input table with data values chopped by col extent:**
::

  >>> table = """
  ...   Col1  |  Col2 |
  ...   1.2       "hello"
  ...   2.4   sdf's worlds
  ... """
  >>> ascii.read(table, format='fixed_width')
  <Table length=2>
    Col1    Col2
  float64   str7
  ------- -------
      1.2    "hel
      2.4 df's wo

**Table with double delimiters:**
::

  >>> table = """
  ... || Name ||   Phone ||         TCP||
  ... |  John  | 555-1234 |192.168.1.10X|
  ... |  Mary  | 555-2134 |192.168.1.12X|
  ... |   Bob  | 555-4527 | 192.168.1.9X|
  ... """
  >>> ascii.read(table, format='fixed_width')
  <Table length=3>
  Name  Phone       TCP
  str4   str8      str12
  ---- -------- ------------
  John 555-1234 192.168.1.10
  Mary 555-2134 192.168.1.12
   Bob 555-4527  192.168.1.9

**Table with space delimiter:**
::

  >>> table = """
  ...  Name  --Phone-    ----TCP-----
  ...  John  555-1234    192.168.1.10
  ...  Mary  555-2134    192.168.1.12
  ...   Bob  555-4527     192.168.1.9
  ... """
  >>> ascii.read(table, format='fixed_width', delimiter=' ')
  <Table length=3>
  Name --Phone- ----TCP-----
  str4   str8      str12
  ---- -------- ------------
  John 555-1234 192.168.1.10
  Mary 555-2134 192.168.1.12
   Bob 555-4527  192.168.1.9

**Table with no header row and auto-column naming:**

Use ``header_start`` and ``data_start`` keywords to indicate no header line.
::

  >>> table = """
  ... |  John  | 555-1234 |192.168.1.10|
  ... |  Mary  | 555-2134 |192.168.1.12|
  ... |   Bob  | 555-4527 | 192.168.1.9|
  ... """
  >>> ascii.read(table, format='fixed_width',
  ...            header_start=None, data_start=0)
  <Table length=3>
  col1   col2       col3
  str4   str8      str12
  ---- -------- ------------
  John 555-1234 192.168.1.10
  Mary 555-2134 192.168.1.12
   Bob 555-4527  192.168.1.9

**Table with no header row and with col names provided:**

Second and third rows also have hanging spaces after final "|". Use
header_start and data_start keywords to indicate no header line.
::

  >>> table = ["|  John  | 555-1234 |192.168.1.10|",
  ...          "|  Mary  | 555-2134 |192.168.1.12|  ",
  ...          "|   Bob  | 555-4527 | 192.168.1.9|  "]
  >>> ascii.read(table, format='fixed_width',
  ...            header_start=None, data_start=0,
  ...            names=('Name', 'Phone', 'TCP'))
  <Table length=3>
  Name  Phone       TCP
  str4   str8      str12
  ---- -------- ------------
  John 555-1234 192.168.1.10
  Mary 555-2134 192.168.1.12
   Bob 555-4527  192.168.1.9


Fixed Width No Header
---------------------

**Table with no header row and auto-column naming. Use the
``fixed_width_no_header`` format for convenience:**
::

  >>> table = """
  ... |  John  | 555-1234 |192.168.1.10|
  ... |  Mary  | 555-2134 |192.168.1.12|
  ... |   Bob  | 555-4527 | 192.168.1.9|
  ... """
  >>> ascii.read(table, format='fixed_width_no_header')
  <Table length=3>
  col1   col2       col3
  str4   str8      str12
  ---- -------- ------------
  John 555-1234 192.168.1.10
  Mary 555-2134 192.168.1.12
   Bob 555-4527  192.168.1.9

**Table with no delimiter with column start and end values specified:**

This uses the col_starts and col_ends keywords. Note that the
col_ends values are inclusive so a position range of zero to five
will select the first six characters.
::

  >>> table = """
  ... #    5   9     17  18      28    <== Column start / end indexes
  ... #    |   |       ||         |    <== Column separation positions
  ...   John   555- 1234 192.168.1.10
  ...   Mary   555- 2134 192.168.1.12
  ...    Bob   555- 4527  192.168.1.9
  ... """
  >>> ascii.read(table, format='fixed_width_no_header',
  ...                 names=('Name', 'Phone', 'TCP'),
  ...                 col_starts=(0, 9, 18),
  ...                 col_ends=(5, 17, 28),
  ...                 )
  <Table length=3>
  Name   Phone      TCP
  str4    str9     str10
  ---- --------- ----------
  John 555- 1234 192.168.1.
  Mary 555- 2134 192.168.1.
   Bob 555- 4527  192.168.1

**Table with no delimiter with only column start or end values specified:**

If only the col_starts keyword is given, it is assumed that each column
ends where the next column starts, and the final column ends at the same
position as the longest line of data.

Conversely, if only the col_ends keyword is given, it is assumed that the first
column starts at position zero and that each successive column starts
immediately after the previous one.

The two examples below read the same table and produce the same result.
::

  >>> table = """
  ... #1       9        19                <== Column start indexes
  ... #|       |         |                <== Column start positions
  ... #<------><--------><------------->  <== Inferred column positions
  ...   John   555- 1234 192.168.1.10
  ...   Mary   555- 2134 192.168.1.123
  ...    Bob   555- 4527  192.168.1.9
  ...    Bill  555-9875  192.255.255.255
  ... """
  >>> ascii.read(table,
  ...                 format='fixed_width_no_header',
  ...                 names=('Name', 'Phone', 'TCP'),
  ...                 col_starts=(1, 9, 19),
  ...                 )
  <Table length=4>
  Name   Phone         TCP
  str4    str9        str15
  ---- --------- ---------------
  John 555- 1234    192.168.1.10
  Mary 555- 2134   192.168.1.123
   Bob 555- 4527     192.168.1.9
  Bill  555-9875 192.255.255.255

  >>> ascii.read(table,
  ...                 format='fixed_width_no_header',
  ...                 names=('Name', 'Phone', 'TCP'),
  ...                 col_ends=(8, 18, 32),
  ...                 )
  <Table length=4>
  Name   Phone        TCP
  str4    str9       str14
  ---- --------- --------------
  John 555- 1234   192.168.1.10
  Mary 555- 2134  192.168.1.123
   Bob 555- 4527    192.168.1.9
  Bill  555-9875 192.255.255.25


Fixed Width Two Line
--------------------

**Typical fixed-format table with two header lines with some cruft:**
::

  >>> table = """
  ...   Col1    Col2
  ...   ----  ---------
  ...    1.2xx"hello"
  ...   2.4   's worlds
  ... """
  >>> ascii.read(table, format='fixed_width_two_line')
  <Table length=2>
    Col1     Col2
  float64    str9
  ------- ---------
      1.2   "hello"
      2.4 's worlds

..
  EXAMPLE END

..
  EXAMPLE START
  Reading a reStructuredText Table

**reStructuredText table:**
::

  >>> table = """
  ... ======= ===========
  ...   Col1    Col2
  ... ======= ===========
  ...   1.2   "hello"
  ...   2.4   's worlds
  ... ======= ===========
  ... """
  >>> ascii.read(table, format='fixed_width_two_line',
  ...                 header_start=1, position_line=2, data_end=-1)
  <Table length=2>
    Col1     Col2
  float64    str9
  ------- ---------
      1.2   "hello"
      2.4 's worlds

..
  EXAMPLE END

**Text table designed for humans and test having position line before the header line:**
::

  >>> table = """
  ... +------+----------+
  ... | Col1 |   Col2   |
  ... +------|----------+
  ... |  1.2 | "hello"  |
  ... |  2.4 | 's worlds|
  ... +------+----------+
  ... """
  >>> ascii.read(table, format='fixed_width_two_line', delimiter='+',
  ...                 header_start=1, position_line=0, data_start=3, data_end=-1)
  <Table length=2>
    Col1     Col2
  float64    str9
  ------- ---------
      1.2   "hello"
      2.4 's worlds

Writing
=======

..
  EXAMPLE START
  Writing Fixed-Width Tables

Fixed Width
-----------

**Define input values ``dat`` for all write examples:**
::

  >>> table = """
  ... | Col1 |  Col2     |  Col3 | Col4 |
  ... | 1.2  | "hello"   |  1    | a    |
  ... | 2.4  | 's worlds |  2    | 2    |
  ... """
  >>> dat = ascii.read(table, format='fixed_width')

**Write a table as a normal fixed-width table:**
::

  >>> ascii.write(dat, format='fixed_width')
  | Col1 |      Col2 | Col3 | Col4 |
  |  1.2 |   "hello" |    1 |    a |
  |  2.4 | 's worlds |    2 |    2 |

**Write a table as a fixed-width table with no padding:**
::

  >>> ascii.write(dat, format='fixed_width', delimiter_pad=None)
  |Col1|     Col2|Col3|Col4|
  | 1.2|  "hello"|   1|   a|
  | 2.4|'s worlds|   2|   2|

**Write a table as a fixed-width table with no bookend:**
::

  >>> ascii.write(dat, format='fixed_width', bookend=False)
  Col1 |      Col2 | Col3 | Col4
   1.2 |   "hello" |    1 |    a
   2.4 | 's worlds |    2 |    2

**Write a table as a fixed-width table with no delimiter:**
::

  >>> ascii.write(dat, format='fixed_width', bookend=False, delimiter=None)
  Col1       Col2  Col3  Col4
   1.2    "hello"     1     a
   2.4  's worlds     2     2

**Write a table as a fixed-width table with no delimiter and formatting:**
::

  >>> ascii.write(dat, format='fixed_width',
  ...                  formats={'Col1': '%-8.3f', 'Col2': '%-15s'})
  |     Col1 |            Col2 | Col3 | Col4 |
  | 1.200    | "hello"         |    1 |    a |
  | 2.400    | 's worlds       |    2 |    2 |

Fixed Width No Header
---------------------

**Write a table as a normal fixed-width table:**
::

  >>> ascii.write(dat, format='fixed_width_no_header')
  | 1.2 |   "hello" | 1 | a |
  | 2.4 | 's worlds | 2 | 2 |

**Write a table as a fixed-width table with no padding:**
::

  >>> ascii.write(dat, format='fixed_width_no_header', delimiter_pad=None)
  |1.2|  "hello"|1|a|
  |2.4|'s worlds|2|2|

**Write a table as a fixed-width table with no bookend:**
::

  >>> ascii.write(dat, format='fixed_width_no_header', bookend=False)
  1.2 |   "hello" | 1 | a
  2.4 | 's worlds | 2 | 2

**Write a table as a fixed-width table with no delimiter:**
::

  >>> ascii.write(dat, format='fixed_width_no_header', bookend=False,
  ...                  delimiter=None)
  1.2    "hello"  1  a
  2.4  's worlds  2  2

Fixed Width Two Line
--------------------

**Write a table as a normal fixed-width table:**
::

  >>> ascii.write(dat, format='fixed_width_two_line')
  Col1      Col2 Col3 Col4
  ---- --------- ---- ----
   1.2   "hello"    1    a
   2.4 's worlds    2    2

**Write a table as a fixed width table with space padding and '=' position_char:**
::

  >>> ascii.write(dat, format='fixed_width_two_line',
  ...                  delimiter_pad=' ', position_char='=')
  Col1        Col2   Col3   Col4
  ====   =========   ====   ====
   1.2     "hello"      1      a
   2.4   's worlds      2      2

**Write a table as a fixed-width table with no bookend:**
::

  >>> ascii.write(dat, format='fixed_width_two_line', bookend=True, delimiter='|')
  |Col1|     Col2|Col3|Col4|
  |----|---------|----|----|
  | 1.2|  "hello"|   1|   a|
  | 2.4|'s worlds|   2|   2|

..
  EXAMPLE END

Custom Header Rows
==================

The ``fixed_width``, ``fixed_width_two_line``, and ``rst`` formats normally include a
single row with the column names in the header.  However, for these formats you can
customize the column attributes which appear as header rows. The available
column attributes are ``name``, ``dtype``, ``format``, ``description`` and
``unit``.  This is done by listing the desired the header rows using the
``header_rows`` keyword argument.

..
  EXAMPLE START
  Custom Header Rows with Fixed Width

::
    >>> from astropy.table.table_helpers import simple_table
    >>> dat = simple_table(size=3, cols=4)
    >>> dat["a"].info.unit = "m"
    >>> dat["d"].info.unit = "m/s"
    >>> dat["b"].info.format = ".2f"
    >>> dat["c"].info.description = "C column"
    >>> ascii.write(
    ...    dat,
    ...    format="fixed_width",
    ...    header_rows=["name", "unit", "format", "description"],
    ... )
    |     a |       b |        c |     d |
    |     m |         |          | m / s |
    |       |     .2f |          |       |
    |       |         | C column |       |
    |     1 |    1.00 |        c |     4 |
    |     2 |    2.00 |        d |     5 |
    |     3 |    3.00 |        e |     6 |

In this example the 1st row is the ``name``, the 2nd row is the ``unit``, and
so forth. You must supply the ``name`` value in the ``header_rows`` list in
order to get an output with the column name included.

A table with non-standard header rows can be read back in the same way, using
the same list of ``header_rows``::

    >>> txt = """\
    ... | int32 | float32 |      <U4 | uint8 |
    ... |     a |       b |        c |     d |
    ... |     m |         |          | m / s |
    ... |       |     .2f |          |       |
    ... |       |         | C column |       |
    ... |     1 |    1.00 |        c |     4 |
    ... |     2 |    2.00 |        d |     5 |
    ... |     3 |    3.00 |        e |     6 |
    ... """
    >>> dat = ascii.read(
    ...     txt,
    ...     format="fixed_width",
    ...     header_rows=["dtype", "name", "unit", "format", "description"],
    ... )
    >>> dat.info
    <Table length=3>
    name  dtype   unit format description
    ---- ------- ----- ------ -----------
    a   int32     m
    b float32          .2f
    c    str4                 C column
    d   uint8 m / s

..
  EXAMPLE END

..
  EXAMPLE START
  Custom Header Rows with Fixed Width Two Line

The same idea can be used with the ``fixed_width_two_line`` format::

    >>> txt = """\
    ...     a       b        c     d
    ... int64 float64      <U1 int64
    ...     m                  m / s
    ... ----- ------- -------- -----
    ...     1    1.00        c     4
    ...     2    2.00        d     5
    ...     3    3.00        e     6
    ... """
    >>> dat = ascii.read(
    ...     txt,
    ...     format="fixed_width_two_line",
    ...     header_rows=["name", "dtype", "unit"],
    ... )
    >>> dat
    <Table length=3>
      a      b     c     d
      m                m / s
    int64 float64 str1 int64
    ----- ------- ---- -----
        1     1.0    c     4
        2     2.0    d     5
        3     3.0    e     6

..
  EXAMPLE END

Note that the ``two_line`` in the ``fixed_width_two_line`` format name refers to
the default situation where the header consists two lines, a row of column names
and a row of separator lines. This is a bit of a misnomer when using
``header_rows``.