Formatting Console Exit

I am having trouble correctly aligning text text in python. I tried everything I knew, but still the same result, and it is very annoying !.

Here is what I get in the console enter image description here

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 
+6
source share
5 answers

Use %45s to create the correct field, which is 45 characters long. And use %-45s to make a left justified string. Also think about extracting your print line into a function - this way you can easily change it 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" 
+12
source

Instead of tabs, it is better to use line formatting directives, where you can specify the exact number of spaces and reserve for output.

for instance

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

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

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

Numbers can be specified using d for integers and f for float values. For instance.

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

In this example, I reserved 3 null spaces for an integer value and 5 spaces for float (2 for values ​​after decimal space). You can also control alignemnt with - . There are many other options for formatting the output, see Suggested links below for more details.

Please note that using the .format() function is .format() . See SO my answer here for how to use .format() . This answer may also help.

Although .format may seem very verbose, it offers great flexibility, and one of my favorite features is the ability to easily print large numbers of commas for easy reading.

For more information, see Python's String Formatting Docs to help you build your output exactly as you would like.

+4
source

You can simplify it if you use string formatting with padding , e.g.

 >>> '%20s' % 'test' ' test' >>> '%-20s' % 'test' 'test ' 
+2
source

I suggest using something 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 
+2
source

You can use a third-party module to format the output to the console.

i.e.: texttable

0
source

Source: https://habr.com/ru/post/919242/


All Articles