3

numpy.savetxtの周りに小さなラッパーを使用して、ヘッダー名を自動的に生成し、読み取り可能な出力用にある程度インテリジェントな幅の配置を作成します。より基本的な解決策は、この答えにあります。

ドキュメントに示されているように左揃えにするのではなく、幅を指定して出力テキストを中央に揃える方法を知りたいです。

numpy.savetxtドキュメントから、私はこの情報を見ます:

Notes
-----
Further explanation of the `fmt` parameter
(``%[flag]width[.precision]specifier``):
flags:
    ``-`` : left justify

    ``+`` : Forces to preceed result with + or -.

    ``0`` : Left pad the number with zeros instead of space (see width).

width:
    Minimum number of characters to be printed. The value is not truncated
    if it has more characters.

ドキュメントは、 python mini形式の仕様で、より「網羅的なリソース」を示していますが、そこにある情報は、アライメント情報と互換性がありません。

さまざまな配置オプションの意味は次のとおりです。

Option  Meaning
'<'     Forces the field to be left-aligned within the available space (this is the default for most objects).
'>'     Forces the field to be right-aligned within the available space (this is the default for numbers).
'='     Forces the padding to be placed after the sign (if any) but before the digits. This is used for printing fields in the form ‘+000000120’. This alignment option is only valid for numeric types.
'^'     Forces the field to be centered within the available space.

'^'非互換性は、savetxtが有効なフォーマット文字として受け入れられないためです。出力が中央揃えになるように`numpy.savetxt'でフォーマットを指定する方法に誰かが光を当てることができますか?

4

1 に答える 1

1

'^'次を使用して、中央揃えフラグを含むより複雑な形式オプションを組み合わせることができますformat

import numpy as np
a = np.ones((3,3))*100
a[1,1]=111.12321
a[2,2]=1
np.savetxt('tmp.txt',a, fmt='{:*^10}'.format('%f'))

与える:

****100.000000**** ****100.000000**** ****100.000000****
****100.000000**** ****111.123210**** ****100.000000****
****100.000000**** ****100.000000**** ****1.000000****
于 2013-05-09T19:59:36.577 に答える