Formatting console Output

落花浮王杯 提交于 2019-12-18 04:00:21

问题


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

Here is the Code I have.

print " FileName\t\t\t\t\tStatus\t\tBinary Type\n"  

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

    print" %s \t\t\t\t  FAILED \t\t        " %files

print "\n\n

回答1:


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"\n"   

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

print "\n\n"



回答2:


In place of the tabs, it's better to use string formatting directives where it's possible to specify the exact number of spaces and to reserve for output.

For instance

print '>>%20s<<' %('some string')
>>         some string<<

will reserve 20 spaces and right-align this string for display. You can left-justify it by using %-20s

print '>>%-20s<<' %('some string')
>>some string         <<

Numbers can be specified using d for integers and f for float values. E.g.,

 print '%03d %5.2f' %(5, 22/7.0)
 005  3.14

In this example I reserved 3 zero-padded spaces for an integer value, and 5 spaces for a float (2 for values after the decimal space). You can control alignemnt here too with -. There are many other options to format output, see the suggested links below for more details.

Note that the use of the .format() function is encouraged. See SO my answer here about how to use .format(). This answer may also help.

While .format may seem very verbose, it does offer a great deal of flexibility and one of my favorite features is the ability to easily print commas in large numbers for readability.

See Python docs on String Formatting for much more details to help you line up your output just exactly the way you'd like it.




回答3:


You can simplify it a lot if you use string formatting with padding, for eg.

>>> '%20s' % 'test'
'                test'
>>> '%-20s' % 'test'
'test                '



回答4:


i suggest using ljust with them some thing like

print files.ljust(20) + 'Passed'.ljust(5) + '64-bit'  

instead of this :

print" %s \t\t\t\tPASSED\t\t 64-bit \t\t  " %files



回答5:


You could use a third party module for formatting the output to the console.

i.e.: texttable



来源:https://stackoverflow.com/questions/11245381/formatting-console-output

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!