格式化和对齐控制台输出

时间:2014-09-11 03:45:10

标签: python

我有一个小的python程序,它以文件名作为参数,当它发现它在控制台上打印表时,它会浏览给定的.txt文件。但该表似乎很乱,如何控制出现在控制台上的输出文本格式?

我的计划如下:

#!/usr/bin/python
import sys
import os

filename=sys.argv[1]
filedir=os.getcwd()

string= 'RADIANS'
count=0
N=11
flag=0

f = open(path,'r')
if flag==0:
    print ('Scanning for FATAL errors---NONE')
    print('Scanning for SYSTEM FATAL errors---NONE')
    print('----------------------------------------Structure EigenFrequncies--------------------------------------------\n')
    newsearch = 'NUMBER OF ROOTS'
    for line in f:
        if newsearch in line:
            print line
            break
    print('------------------------------------- REAL EIGEN VALUES(Displaying first 10 modes)-------------------------------\n')
for line in f:
    if flag==0:
        if string in line:
            print line
            for i in range(N):
                if exitstring1 not in line:
                    line = f.next().strip()
                    print line
                else:
                    break
            break
f.close()

文本文件如下:

                          E I G E N V A L U E  A N A L Y S I S   S U M M A R Y   (READ MODULE) 



                                     BLOCK SIZE USED ......................    7

                                     NUMBER OF DECOMPOSITIONS .............    3

                                     NUMBER OF ROOTS FOUND ................   46

                                     NUMBER OF SOLVES REQUIRED ............   33

1    EXTSE REDUCTION RUN                                                       JULY   1, 2014  NX NASTRAN  5/ 1/14   PAGE    10
      SE_10_KGH_09_5000HZ                                                                                                           
0                                                                                                                                   

                                              R E A L   E I G E N V A L U E S
                                         (BEFORE AUGMENTATION OF RESIDUAL VECTORS)
   MODE    EXTRACTION      EIGENVALUE            RADIANS             CYCLES            GENERALIZED         GENERALIZED
    NO.       ORDER                                                                       MASS              STIFFNESS
        1         1        1.858571E+08        1.363294E+04        2.169750E+03        1.000000E+00        1.858571E+08
        2         2        2.912237E+08        1.706528E+04        2.716023E+03        1.000000E+00        2.912237E+08
        3         3        4.555573E+08        2.134379E+04        3.396969E+03        1.000000E+00        4.555573E+08
        4         4        4.794632E+08        2.189665E+04        3.484960E+03        1.000000E+00        4.794632E+08
        5         5        4.850065E+08        2.202286E+04        3.505047E+03        1.000000E+00        4.850065E+08
        6         6        4.879794E+08        2.209025E+04        3.515773E+03        1.000000E+00        4.879794E+08
        7         7        4.898815E+08        2.213327E+04        3.522619E+03        1.000000E+00        4.898815E+08
        8         8        4.968964E+08        2.229117E+04        3.547750E+03        1.000000E+00        4.968964E+08
        9         9        5.004465E+08        2.237066E+04        3.560401E+03        1.000000E+00        5.004465E+08
       10        10        5.088724E+08        2.255820E+04        3.590249E+03        1.000000E+00        5.088724E+08

但是当我运行我的代码时,输​​出似乎是这样的:

 MODE    EXTRACTION      EIGENVALUE            RADIANS             CYCLES            GENERALIZED         GENERALIZED

NO.       ORDER                                                                       MASS              STIFFNESS
1         1        2.292081E+04        1.513962E+02        2.409545E+01        1.000000E+00        2.292081E+04
2         2        2.701519E+04        1.643630E+02        2.615918E+01        1.000000E+00        2.701519E+04
3         3        5.071461E+04        2.251991E+02        3.584154E+01        1.000000E+00        5.071461E+04
4         4        5.426810E+04        2.329551E+02        3.707596E+01        1.000000E+00        5.426810E+04
5         5        1.084471E+05        3.293130E+02        5.241179E+01        1.000000E+00        1.084471E+05
6         6        1.195545E+05        3.457666E+02        5.503046E+01        1.000000E+00        1.195545E+05
7         7        1.254440E+05        3.541807E+02        5.636961E+01        1.000000E+00        1.254440E+05
8         8        3.216040E+05        5.671014E+02        9.025700E+01        1.000000E+00        3.216040E+05
9         9        3.434422E+05        5.860394E+02        9.327106E+01        1.000000E+00        3.434422E+05
10        10        3.545295E+05        5.954238E+02        9.476464E+01        1.000000E+00        3.545295E+05

我如何控制对齐?任何人都可以帮我这个吗?

1 个答案:

答案 0 :(得分:0)

我假设您希望输出看起来与输入表的输出相同?

问题是您正在剥离数据行,而不是第一个标题行。对于数据行,剥离会删除任何尾随的新行字符,然后print会在输出中添加一个新行。对于第一个标题行,您剥离它,因此当您打印它时,最终会有2个新行(这就是为什么标题行之间有一个空行。)

剥离还会删除任何前导空格,这也会影响标题和数据行的对齐。

要解决此问题,您可以使用rstrip()来删除该行的 end 中的空格(包括新行)。

#!/usr/bin/python
import sys
import os

filename=sys.argv[1]
filedir=os.getcwd()

exitstring1 = "something that won't be found"
string= 'RADIANS'
count=0
N=11
flag=0

f = open(filename,'r')
if flag==0:
    print ('Scanning for FATAL errors---NONE')
    print('Scanning for SYSTEM FATAL errors---NONE')
    print('----------------------------------------Structure EigenFrequncies--------------------------------------------\n')
    newsearch = 'NUMBER OF ROOTS'
    for line in f:
        if newsearch in line:
            print line.rstrip()
            break
    print('------------------------------------- REAL EIGEN VALUES(Displaying first 10 modes)-------------------------------\n')
for line in f:
    if flag==0:
        if string in line:
            print line,
            for i in range(N):
                if exitstring1 not in line:
                    line = f.next().rstrip()
                    print line
                else:
                    break
            break
f.close()

请注意,为exitstring1添加了一个值,因为这未在您的代码中定义。

另一种方法是使用sys.stdout.write(line)而不是打印,而不是剥离。