我有这段代码
print("{0:<10} {1:>25}".format("Name", "RAM"))
for droplet in droplets:
print("{0:<10} {1:>25}".format(droplet.name, droplet.memory))
我希望它打印出一个像这样的好列:
Name RAM
example-droplet 1024
ubuntu-s-1vcpu-1gb-nyc1-01 1024
但我得到了这个:
Name RAM
example-droplet 1024
ubuntu-s-1vcpu-1gb-nyc1-01 1024
如何获得所需的输出?
答案 0 :(得分:3)
你缩进了错误的一面。试试这个:
print("{0:<30}{1:<10}".format("Name", "RAM"))
for droplet in droplets:
print("{0:<30}{1:<10}".format(droplet.name, droplet.memory))
打印
Name RAM
example-droplet 1024
ubuntu-s-1vcpu-1gb-ncy1-01 1024
对我来说。
答案 1 :(得分:2)
您可以尝试以下
print("{0:35} {1}".format("Name", "RAM"))
for droplet in droplets:
print("{0:35} {1}".format(droplet.name, droplet.memory))
通过更改水滴进行测试来映射:
print("{0:35} {1}".format("Name", "RAM"))
droplets = [{'name': 'example-droplet', 'memory':1024}, {'name': 'ubuntu-s-1vcpu-1gb-nyc1-01', 'memory':256}]
for droplet in droplets:
print("{0:35} {1}".format(droplet['name'], droplet['memory']))
结果:
Name RAM
example-droplet 1024
ubuntu-s-1vcpu-1gb-nyc1-01 256