0

I'm currently trying to rewrite some tools in python, the only challenge is that I'm limited to Python 2.4.

The current output from the bash version looks like this:

Host           Active VMs     SW            Ver       Processor        Cores     Mhz          Tot Mem     Free Mem     Network
cloudmatt1     1              XenServer     1.8.0     GenuineIntel     2         2660.040     5.45G       3.57G        openvswitch     
cloudmatt2     3              XenServer     1.8.0     GenuineIntel     2         2660.046     5.45G       1.55G        openvswitch  

Here is my example dictionary:

{'d50787a5-9187-4486-9d36-e8287701bcbd': {'UUID': 'd50787a5-9187-4486-9d36-e8287701bcbd', 'Network-Backend': 'openvswitch', 'Platform-Version': '1.8.0', 'Host-CPUs': 2, 'Name-label': 'cloudmatt1', 'Resident-VMs': 2}, 'e9e54df0-ab87-4e68-a604-2e1821ce2da9': {'UUID': 'e9e54df0-ab87-4e68-a604-2e1821ce2da9', 'Network-Backend': 'openvswitch', 'Platform-Version': '1.8.0', 'Host-CPUs': 2, 'Name-label': 'cloudmatt2', 'Resident-VMs': 4}}

How could I display the data in a similar fashion with Python 2.4?

0

2 Answers 2

0

Looks like you have some left-aligned columns, each with a specific size. You can get a similar effect from Python's string formatting. Example:

>>> print "%-10s %-3s %-10s" % ('foo', 2, 'bar')
foo        2   bar

You can decide how wide your columns need to be dynamically using asterisks:

>>> print "%-*s %-*s %-*s" % (10, 'foo', 3, 2, 10, 'bar')
foo        2   bar

In this case it's probably easier just to figure out ahead of time how wide you want the columns, but you could also check the length of each row's values to figure out how wide the columns should be.

Note: Checked this in 2.5, but these should probably work in 2.4 as well.

Sign up to request clarification or add additional context in comments.

Comments

0

With Python 2.4 you can left-align text using the %sN where N is the padding you want to use. e.g:

print "%s-20" % "foo"

1 Comment

I believe that you meant to write '%-20s'.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.