2014-09-11 77 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