【问题标题】:List stored in variable converted to dictionaries存储在变量中的列表转换为字典
【发布时间】:2013-08-26 14:52:52
【问题描述】:

我正在使用以下方法在 Windows 端点上执行 WMI 查询,该查询以列表形式返回结果。我现在想将该列表转换为字典 key:value 以便我可以搜索所有以“Name”为名称的键,并将返回:“ASPNET”“Guest”“Admin”。

import wmi_client_wrapper as wmi

wmic = wmi.WmiClientWrapper(
username="corp.testdomain.com/Administrator",
password="fakepassword",
host="192.168.1.100",
)

output = wmic.query("Select * from Win32_UserAccount Where LocalAccount = True")


{'Status': 'OK', 'Domain': 'localhost', 'Description': 'Account used for running the ASP.NET worker process (aspnet_wp.exe                      )', 'InstallDate': None, 'Caption': 'localhost\\ASPNET', 'Disabled': False, 'PasswordChangeable': False, 'Lockout': False,                       'AccountType': '512', 'SID': '45474748484848-1002', 'LocalAccount': True, 'FullName': 'ASP.NET Ma                      chine Account', 'SIDType': '1', 'PasswordRequired': False, 'PasswordExpires': False, 'Name': 'ASPNET'}
{'Status': 'Degraded', 'Domain': 'localhost', 'Description': 'Built-in account for guest access to the computer/domain', '                      InstallDate': None, 'Caption': 'localhost\\Guest', 'Disabled': True, 'PasswordChangeable': False, 'Lockout': False, 'Accou                      ntType': '512', 'SID': '3645747474747858-501', 'LocalAccount': True, 'FullName': '', 'SIDType': '1',                       'PasswordRequired': False, 'PasswordExpires': False, 'Name': 'Guest'}
{'Status': 'OK', 'Domain': 'localhost', 'Description': 'Built-in account for administering the computer/domain', 'InstallD                      ate': None, 'Caption': 'localhost\\sol2112', 'Disabled': False, 'PasswordChangeable': True, 'Lockout': False, 'AccountType                      ': '512', 'SID': '834668384636846843-500, 'LocalAccount': True, 'FullName': '', 'SIDType': '1', 'Pass                      wordRequired': True, 'PasswordExpires': False, 'Name': 'Admin'}

【问题讨论】:

  • output后面的三个字典是不是作为输出内容的结果列表?
  • 我不确定你在问什么。请提供一个或多个您想要转换为字典的“列表”的示例,以及您尝试过的一些代码。要求代码的问题必须表明对要解决的问题的最低理解。包括尝试的解决方案、它们为什么不起作用以及预期的结果。另见:Stack Overflow question checklist
  • 那么,如果output 的布局正确,我的回答对你有用吗?

标签: python python-2.7 wmi wmic


【解决方案1】:

你可以这样做

>>> dict1 = {'Status': 'OK', 'Domain': 'localhost', 'Description': 'Account used for running the ASP.NET worker process (aspnet_wp.exe                      )', 'InstallDate': None, 'Caption': 'localhost\\ASPNET', 'Disabled': False, 'PasswordChangeable': False, 'Lockout': False,                       'AccountType': '512', 'SID': '45474748484848-1002', 'LocalAccount': True, 'FullName': 'ASP.NET Ma                      chine Account', 'SIDType': '1', 'PasswordRequired': False, 'PasswordExpires': False, 'Name': 'ASPNET'}
>>> print dict1['Name']
ASPNET

【讨论】:

    【解决方案2】:

    如果output的结构是:

    [{'Status': 'OK', 'Domain': 'localhost', 'Description': 'Account used for running the ASP.NET worker process (aspnet_wp.exe                      )', ...,  'Name': 'ASPNET'},
     {'Status': 'Degraded', 'Domain': 'localhost', 'Description': 'Built-in account for guest access to the computer/domain', ..., 'Name': 'Guest'},
     {'Status': 'OK', 'Domain': 'localhost', 'Description': 'Built-in account for administering the computer/domain',  ..., 'Name': 'Admin'}]
    

    我会将它迭代成一个列表字典,由字典中的键作为键:

    output = wmic.query("Select * from Win32_UserAccount Where LocalAccount = True")
    
    new_dict = {}
    for key in output[0]:
        new_dict[key] = [old_dict[key] for old_dict in output]
    

    这也可以通过两个列表解析和dict 内置函数在一行中完成:

    new_dict = dict([(key, [old_dict[key] for old_dict in output])
                     for key in output[0]])
    

    如果你有字典理解,你可以使用其中之一:

    new_dict = {key: [old_dict[key] for old_dict in output]
                for key in output[0]}
    

    其中每一个都将提供一个字典,该字典与output 中的每个结果字典都具有相同的键控。 output 中字典中的所有值都存在于一个列表中,位于它们在原始字典中的键处。它们按照它们在output 中出现的顺序排列。

    {'Status': ['OK', 'Degraded', 'OK'], 
     'Domain': ['localhost', 'localhost', 'localhost'],
     'Description': ['Account used for running the ASP.NET worker process (aspnet_wp.exe)', 
                     'Built-in account for guest access to the computer/domain',
                     'Built-in account for administering the computer/domain'],
     'InstallDate': [None, None, None],
     'Caption': ['localhost\\ASPNET', 'localhost\\Guest', 'localhost\\sol2112'],
     'Disabled': [False, True, False],
     'PasswordChangeable': [False, False, True],
     'Lockout': [False, False, False],
     'AccountType': ['512', '512', '512'],
     'SID': ['45474748484848-1002',
             '3645747474747858-501',
             '834668384636846843-500'], 
     'LocalAccount': [True, True, True],
     'FullName': ['ASP.NET Machine Account', '', ''], 
     'SIDType': ['1', '1', '1'], 
     'PasswordRequired': [False, False, True],
     'PasswordExpires': [False, False, False],
     'Name': ['ASPNET', 'Guest', 'Admin']}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-11-02
      • 2019-08-10
      • 2021-09-06
      • 1970-01-01
      • 1970-01-01
      • 2017-04-23
      • 2021-10-13
      相关资源
      最近更新 更多