2016-08-24 122 views
0

我使用novaclient從Openstack獲取詳細信息。我能夠檢索信息,但是我想將其轉換爲json格式。我使用「to_dic()」,但它在「server_details = server_id_name.to_dict()」上拋出「Attribute」錯誤,不知道爲什麼。json格式的Openstack輸出

AttributeError: "'tuple' object has no attribute 'to_dict'" 

代碼如下給出

from novaclient import client as novaclient 
import json 

nova = novaclient.Client(version='2.0',username='xxxx',api_key='xxxx',project_id='xxxx',auth_url='http://192.168.12.3:5000/v2.0/',insecure='True') 

server_details = dict() 
server = nova.servers.list() 
for server in nova.servers.list(): 
    print server.id, server.name 
    server_id_name = server.id, server.name 
    server_details = server_id_name.to_dict() 
    for network in server.networks.items(): 
     print network 

回答

1

它在你的Python代碼錯誤。 你應該使用

server_details[server.id] = server.name 

更換你的代碼

server_id_name = server.id, server.name 
server_details = server_id_name.to_dict() 

BTW,你應該瞭解蟒蛇。如果你想更改元組爲字典,你應該看到python-tuple-to-dict

+0

謝謝並同意,我是新來的蟒蛇! – tgcloud