astropy:docs
  • Index
  • Modules

ナビゲーション

  • next »
  • « previous |
  • Astropy v4.3.1 »
  • ASCII表 (astropy.io.ascii ) »
  • 固定幅画廊

固定幅画廊¶

固定幅テーブルとは、テーブル中の各列の各行が同じ幅を有するテーブルである。これは,一般に表を人間やFortranコードが読みやすいようにするために用いられる.引用符や特殊文字の問題も減少します例えば:

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

固定幅表のフォーマットには多くの一般的な変形がありますこれらの変形は astropy.io.ascii 読むことができて書くことができます。最も顕著な違いは見出し行がないかどうかである (FixedWidthNoHeader )、見出し行 (FixedWidth )または2つの見出し行 (FixedWidthTwoLine )である。次に,セパレータが両端に現れているかどうか(“bookends”)や,セパレータの周囲にパディングがあるかどうかなど,セパレータ文字が変化する.

詳細な情報は、クラスAPI文書において見つけることができるが、すべてのオプションおよびそのインタラクションを理解する最も簡単な方法は、通過例である。

本を読む¶

FixedWidth¶

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

データ値が列範囲で切断された奇妙な入力テーブル: **

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

2つのセパレータを持つ表: **

>>> 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 = """
...  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:

使用 header_start そして data_start 見出しなし行のキーワードを指示する.**

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

タイトル行がなく、列名が提供されるテーブル:

2行目と3行目は最後の“|”の後にもサスペンションスペースがあります。HEADER_STARTおよびDATA_STARTキーワードを使用して、タイトル行がないことを示す。**

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

FixedWidthNoHeader¶

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

区切りがなく、列の開始値および終了値が指定された表:

COL_STARTSとCOL_ENDキーワードを使用する。Colends値は含まれているため、0から5までの位置範囲は上位6文字が選択されることに注意されたい。**

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

デリミタのないテーブルは、列の開始値または終了値のみを指定する。

COOL_STARTSキーワードのみが与えられている場合、各列は次の列の開始で終了し、最後の列は最長データ行と同じ位置で終了すると仮定する。

逆に、COLENDキーワードのみが与えられた場合、第1の列は位置ゼロから始まり、各列は前の列の直後にあると仮定する。

以下の2つの例は、同じテーブルを読み出し、同じ結果を生成する。**

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

FixedWidthTwoLine¶

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

ReStrucureText表: **

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

人間とテストのために設計されたテキスト表は、タイトル行の前に位置行があります。 **

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

文章を書く¶

FixedWidth¶

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 |

FixedWidthNoHeader¶

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

FixedWidthTwoLine¶

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|

Page Contents

  • 固定幅画廊
    • 本を読む
      • FixedWidth
      • FixedWidthNoHeader
      • FixedWidthTwoLine
    • 文章を書く
      • FixedWidth
      • FixedWidthNoHeader
      • FixedWidthTwoLine

  Back to Top

© Copyright 2011–2021, The Astropy Developers.
Created using Sphinx 4.3.1.   Last built 18 12月 2021.