2013-03-21 3 views
2

numpy.savetxt 주위에 작은 래퍼를 사용하여 헤더 이름을 자동으로 생성하고 읽을 수있는 출력을 위해 다소 지능적인 너비 정렬을 만듭니다. 더 베어 본 솔루션은 this answer입니다.numpy.savetxt에 의해 생성 된 정렬 텍스트를 가운데 맞춤하는 방법은 무엇입니까?

너비를 지정하고 출력 텍스트를 중앙에 정렬하는 방법을 알고 싶습니다. 또한 문서에 표시된대로 왼쪽 맞춤으로 지정하는 것이 아닙니다. numpy.savetxt 문서에서

,이 정보를 볼 :

Notes 
----- 
Further explanation of the `fmt` parameter 
(``%[flag]width[.precision]specifier``): 
워드 프로세서는 python mini format specification에서 더 '철저한 자원'을 가리
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. 

하지만, 정보를 정렬 호환이 정보. 다음과 같이

다양한 정렬 옵션의 의미는 다음과 같습니다 savetxt 유효한 형식 문자로 '^'을 허용하지 않기 때문에

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. 

비 호환성입니다. `numpy.savetxt '형식을 지정하여 출력이 가운데 정렬되도록하는 방법에 대해 알려줄 수 있습니까?

답변

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.**** ****100.000000**** 
****100.000000**** ****100.000000**** ****1.000000**** 
+0

귀하의 포함을의'****'조금 나를 혼란. 나는 그것을 점검하고, 나의 질문에 답한다. 고맙습니다. 마지막으로'fmt = '{0 :^{1}}'을 사용합니다. format (fmtname, fieldlen)'fmtname'은'np.savetxt'에 대해 받아 들여진 형식 사양 중 하나입니다. – achennu

+0

그래, 방금 집중 효과를 묘사하기 위해 이것을 사용했다 ... 고마워! –

관련 문제