Python 格式化和对齐控制台输出
我有一个小python程序,它以filename作为参数,当它找到给定的.txt文件时,它会在控制台上打印该表。但是表格看起来很凌乱,如何控制控制台上显示的输出文本格式 我的计划如下:Python 格式化和对齐控制台输出,python,Python,我有一个小python程序,它以filename作为参数,当它找到给定的.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: p
#!/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
文本文件如下:
#!/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
如何控制对齐?有人能帮我吗?我假设您希望输出与输入表的输出相同 问题是您正在剥离数据行,但不是第一个标题行。对于数据行,剥离将删除任何尾随的新行字符,然后
print
将新行添加到输出中。对于第一个标题行,您没有将其剥离,因此当您打印它时,您将得到两个新行(这就是为什么标题行之间有一个空行的原因)
剥离还会删除任何前导空格,这也会影响标题和数据行的对齐
要修复此问题,您可以使用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)
而不是打印,而不进行剥离。使用pythontablate
模块我没有安装该tablate模块的root权限,我在一家公司工作,有其他方法吗@合作研发