Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
235 views
in Technique[技术] by (71.8m points)

python - Formatting console Output

I'm having trouble making python print out texts properly aligned. I have tried everything I knew, but still the same result and it's very annoying!.

Here is what I'm getting in the console enter image description here

Here is the Code I have.

print " FileNameStatusBinary Type
"  

for files in PASS:
    log = subprocess.check_output(['dumpbin','/HEADERS',files])
    if arch64 in log:
        print" %s PASSED 64-bit   " %files 
    elif arch32 in log:
        print" %s PASSED 32-bit   " %files
print"
"   
for files in FAILED:

    print" %s   FAILED         " %files

print "


See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Use %45s to make a right justified field that is 45 characters long. And use %-45s to make a left justified string. Also consider extracting your line printing into a function - that way you'll be able to change it easily in one place. Like this:

# fake setup 
PASS = ["foo.exe", "bar.exe", "really_long_filename.exe"]
FAILED = ["failed.exe"]
types = ["32-bit", "64-bit", "64-bit"]
arch64 = '64'
arch32 = '32'

# your code
def print_row(filename, status, file_type):
    print " %-45s %-15s %15s" % (filename, status, file_type)

print_row('FileName', 'Status', 'Binary Type')

for files in PASS:
    log = types.pop()
    if arch64 in log:
        print_row(files, 'PASSED', '64-bit')
    elif arch32 in log:
        print_row(files, 'PASSED', '32-bit')
print"
"   

for files in FAILED:
        print_row(files, 'FAILED', '')

print "

"

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...