繁体   English   中英

如何将class类型转换为字符串python SDK

[英]How to convert class type to string python SDK

我有这段code可以打印与VM相关的信息。 output属于class type 我怎样才能convert它转换成我能理解的形式?

 from azure.common.credentials import ServicePrincipalCredentials
 from azure.mgmt.compute import ComputeManagementClient
    
 credential = ServicePrincipalCredentials(client_id='XXXX',
                                          secret='XXXX', tenant='XXXX')
 compute_client = ComputeManagementClient(
     credential, 'XXXX')
 for vm in compute_client.virtual_machines.list_all():
     print(vm.storage_profile)

我在表格中收到output 它显示了这个outputclass type

<'azure.mgmt.compute.v2019_03_01.models.storage_profile_py3.StorageProfile'>

它正在打印class类型,因为您直接调用variable而不转换它。 为此,您可以创建一个新变量并将转换分配给它并打印该新变量。 或者您可以直接在打印语句中使用,如下所示,

这也是@baileythegreen的建议,感谢您在回答中发布相同的宝贵见解以帮助其他社区成员。

代码:

from azure.common.credentials import ServicePrincipalCredentials
from azure.mgmt.compute import ComputeManagementClient
    
 credential = ServicePrincipalCredentials(client_id='XXXX',
                                          secret='XXXX', tenant='XXXX')
 compute_client = ComputeManagementClient(
     credential, 'XXXX')
 for vm in compute_client.virtual_machines.list_all():
     print(vm.storage_profile.as_dict()) 

有关更多信息,请参阅以下链接:-

SO 线程: Azure Python SDK - 列出虚拟机并生成自定义 JSON 响应

MS Q&A:- 打印虚拟机列表

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM