Python: how to print and format rows from API results

I am very new to python and I am trying to print API results in a nice format. Currently when I print the results which is host information, it prints the information in the format below:

{'ip': u'10.168.1.30', 'id': 3 'name': u'host5.testlab.net'}
{'ip': u'10.168.1.31', 'id': 6 'name': u'host51.testlab.net'}
{'ip': u'10.168.1.50', 'id': 8 'name': u'host6.testlab.net'}
{'ip': u'10.168.1.42', 'id': 5 'name': u'host17.testlab.net'}
and so on...

I would like to have it print like the following if possible. Eventually I am going to try to write to an html file but for now I just want to get a nice format for printing:
IP: 10.168.1.30   ID: 3   Name: host5.testlab.net
IP: 10.168.1.31   ID: 6   Name: host51.testlab.net
and so on..

Below is part of the code that I have. I have omitted some of the code but have put the dict and list that I have created and the print command.
listHost = []
outputlist = []
#Getting API results
for host in listHost['results']
#My Dictionary and list
             info = {}
             info['id'] = host['id']
             info['id'] = host['name']
             info['ip'] = host['ip']

             outputlist.append(info)
for row in outputlist:
             print str(row)
staten12Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

brendanmeyerCommented:
on the last line instead of
print str(row)
try
print 'IP: {0},\tID: {1},\tName: {2}'.format(row['ip'], row['id'], row['name'])


also      info['id'] = host['name']
probably shoud be      info['name'] = host['name']
1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
peprCommented:
This is a good commend by brendanmeyer. Anyway, there is one more readable approach when formatting the things from a dictionary. It is less error prone if you have more items in the dictionary and if you write more string templates with the placehodlers. Try the following:
#!python2

info = {'ip': u'10.168.1.30', 'id': 3, 'name': u'host5.testlab.net'}
print 'IP: {ip}   ID: {id}   Name: {name}'.format(**info)

Open in new window

The difference is that it is possible to use named placeholders instead of the numbered ones -- see the doc https://docs.python.org/2.7/library/stdtypes.html#str.format 

In the case, you need to supply keyword arguments. You can get them from the dictionary using the two stars in front of the name.

In Python 3, you can use new str.format_map(info) instead.
0
staten12Author Commented:
Thank you brendanmeyer. That solution worked. I had attempted to use the .format but did not have it in the correct place. I also had the info['name'] = host['name'] correct. Just a typo when I submitted the question. Again, thanks for your help.
0
staten12Author Commented:
pepr,

Thanks for the information. It will be useful for other python scripts that I will be writing. I didn't include anything inside the {} for info because I first had to get that information from the API results that I was getting.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Python

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.