0

I learned and code below which gives the Hostnames and Its IP address by the reading the hostname from the "mylabList.txt" file, Now i am looking the way to print the output in a pretty Human readable from Like Column header at the top of each and then the name (Below that)..

Is there way to set width between columns while printing...

#!/usr/bin/python

import sys
import socket
with open("mylabList.txt", 'r') as f:

  for host in f:
        print("{0[0]}\t{0[2][0]}".format(socket.gethostbyname_ex(host.rstrip())))

Current output is Like:

mylab1.example.com   172.10.1.1
mylab2.example.com   172.10.1.2
mylab3.example.com   172.10.1.3
mylab4.example.com   122.10.1.4

Expected Output is:

Server Name     IP ADDRESS
===================================
mylab1.example.com      172.10.1.1
mylab2.example.com      172.10.1.2
mylab3.example.com      172.10.1.3
mylab4.example.com      122.10.1.4

Just a note.. in my output Srever Name's lenghth is upto 30 Char long.

  • Have you looked at [tabulate](https://pypi.python.org/pypi/tabulate)? See [this post](https://stackoverflow.com/questions/5909873/python-pretty-printing-ascii-tables) – Cory Kramer May 05 '16 at 15:27
  • http://stackoverflow.com/questions/9535954/python-printing-lists-as-tabular-data – kmaork May 05 '16 at 15:28
  • @CoryKramer ... I dont have tabulate Module in my System, though i am a newbie to python just trying to understand how it be fitted .. –  May 05 '16 at 15:33
  • in addition & specific to your setup, note that you can set the size of the string in [.format()](https://docs.python.org/3/library/stdtypes.html#str.format) like so `{:30}` . A good tutorial is [here](https://pyformat.info/). – patrick May 05 '16 at 15:37
  • @patrick .... I tried that but seems i am poor to catch to stitching it correctly into the current scenario. Though i am trying things continuously –  May 05 '16 at 15:41
  • you can try something like `template="{:30}\t{:30}"`, then print the header like so `print (template.format(server_name, ip_address))` and then fill in the same template in the `for` loop `print (template.format(mylab, ip))`. Like I said, it's an option and the other suggestions are worth looking at if that does not do it. – patrick May 05 '16 at 15:48

1 Answers1

0

You could use ljust and rjust

http://www.tutorialspoint.com/python/string_ljust.htm http://www.tutorialspoint.com/python/string_rjust.htm

print("A String".ljust(30, " ") + "Another String")

results in

A String                      Another String

This is a possible way to do the trick:

#!/usr/bin/python

import sys
import socket
print("Server Name".ljust(30, " ") + "IP ADRESS")
print("="*39)
with open("mylabList.txt", 'r') as f:
    for host in f:
        print("{0[0]}\t{0[2][0]}".format(socket.gethostbyname_ex(host.rstrip())))
BigZ
  • 836
  • 8
  • 19
  • Its somewhat near to the goal, but it does only the printing Header and justify the space between the two Header Strings only not the the output columns. `Server Name IP ADRESS` `=======================================` So, the space between original Servername and IP Address are remain same. –  May 05 '16 at 15:59