2014-09-11 39 views
0

我有一个小型的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

使用python'tabulate'模块 – rjv 2014-09-11 03:48:25

+0

我没有root权限来安装那个制表模块,我在一家公司工作,有没有其他方法可以做到这一点? @rjv – ayaan 2014-09-11 03:59:19

回答

0

我假设你希望你的输出看起来和输入表一致吗?

问题是,您正在剥离数据线,但不是第一个标题行。对于数据行,剥离删除任何尾随的新行字符,然后print向输出添加新行。对于第一个标题行,你剥离它,所以当你打印你结束了2个新行(这就是为什么在标题行之间的空行。)

剥离也消除了任何领先的白色空间,这也会影响标题和数据行的对齐。

要解决您的问题,您可以使用rstrip(),它将从行的结束删除空白(包括新行)。

#!/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)而不是打印,没有剥离。

+0

谢谢,我知道了@ mhawke – ayaan 2014-09-12 02:05:48