【问题标题】:Converting the output of subprocess.check_output() into a dictionary将 subprocess.check_output() 的输出转换为字典
【发布时间】:2016-03-31 08:03:43
【问题描述】:

我正在使用 subprocess.check_output 从 Python 运行命令 netsh wlan show interfaces 并将其收集到一个变量中。

a = subprocess.check_output(["netsh","wlan","show","interfaces"])

它给我的输出是:

>>> print a
>>> \r\nThere is 1 interface on the system: \r\n\r\n    Name                   : Wireless Network Connection\r\n    Description            : Ralink RT3290 802.11bgn Wi-Fi Adapter\r\n    GUID                   : 77cbe2d6-1a1e-41e7-be0a-3f18689c9ceb\r\n    Physical address       : c0:38:96:92:b7:1d\r\n    State                  : connected\r\n    SSID                   : PenguinWiFi\r\n    BSSID                  : 6c:72:20:d2:f9:21\r\n    Network type           : Infrastructure\r\n    Radio type             : 802.11n\r\n    Authentication         : WPA2-Personal\r\n    Cipher                 : CCMP\r\n    Connection mode        : Auto Connect\r\n    Channel                : 11\r\n    Receive rate (Mbps)    : 72\r\n    Transmit rate (Mbps)   : 72\r\n    Signal                 : 100% \r\n    Profile                : PenguinWiFi \r\n\r\n    Hosted network status  : Not available\r\n\r\n'

我想从此输出中获取 wlan 的状态为"connected""not connected"

为此,我想将上述字符串转换为字典,并希望使用"state" 作为键,以便从键中获取值并确定wlan 是否已连接。

但是当我将它转换为字典时,它给出的错误是这样的:

>>> a_dict = dict(a)

Traceback (most recent call last):
  File "<pyshell#4>", line 1, in <module>
    a_dict = dict(a)
ValueError: dictionary update sequence element #0 has length 1; 2 is required

我不知道真正的问题是什么,是字符串还是其他问题?

【问题讨论】:

    标签: python string python-2.7 dictionary subprocess


    【解决方案1】:

    我没有得到真正的问题是字符串还是其他问题?

    您不能只将字符串转换为字典,即使该字符串对人类来说读起来很像。

    您将不得不解析该字符串并从中提取 (key, value) 对。

    关键是例如“物理地址”和值“c0:38:96:92:b7:1d”

    netsh 有一个计算机可读的 XML 输出

    Windows 过滤平台 (WFP) 的 Netsh 命令使您能够 然后,该工具将收集的数据导出到 XML 文件中 您可以检查问题原因的线索。

    为什么不使用XML output 而不是在最微小的控制台输出格式更改时可能停止工作的脆弱黑客?

    netsh wlan export profile folder="PATH_TO_FOLDER" name=PROFILENAME
    

    这应该给你一个计算机可读的文件,可以用Python numerous XML tools轻松遍历。

    从对列表中形成字典

    ValueError: 字典更新序列元素#0 的长度为 1; 2 是 必填

    这是因为dict 需要一个 (key, value) 对的列表,从中形成一个 dict。 (key, value) 长度为 2(是一对)

    例如

    >>> dict([('a', 1), ('b', 2)])
    {'a': 1, 'b': 2}
    

    【讨论】:

    • 非常感谢bakkal,你给的描述非常好..!
    【解决方案2】:

    为什么不简单地使用正则表达式:

    >>> re.search('State\s+: (\w+)', a).group(1)
    'connected'
    >>> 
    

    如果需要将其转换为字典,请使用 re.split() 和一些 ,如下所示:

    >>> dict(map(str.strip, re.split('\s+:\s+', i)) 
             for i in a.strip().splitlines() if i.startswith('    '))
    
    {'State': 'connected',
     'Hosted network status': 'Not available',
     'Network type': 'Infrastructure',
     'Physical address': 'c0:38:96:92:b7:1d', 
     'GUID': '77cbe2d6-1a1e-41e7-be0a-3f18689c9ceb',
     'Authentication': 'WPA2-Personal',
     'Description': 'Ralink RT3290 802.11bgn Wi-Fi Adapter',
     'Radio type': '802.11n',
     'Signal': '100%',
     'Connection mode': 'Auto Connect',
     'SSID': 'PenguinWiFi',
     'Channel': '11',
     'Profile': 'PenguinWiFi',
     'BSSID': '6c:72:20:d2:f9:21',
     'Name': 'Wireless Network Connection',
     'Cipher': 'CCMP', 'Receive rate (Mbps)': '72',
     'Transmit rate (Mbps)': '72'}
    
    
    >>> dict(map(str.strip, re.split('\s+:\s+', i)) 
             for i in a.strip().splitlines() if i.startswith('    '))['State']
    
    'connected'
    

    【讨论】:

    • 我直接使用正则表达式得到了字符串。感谢您的详细描述.. :)
    猜你喜欢
    • 1970-01-01
    • 2020-06-30
    • 2017-10-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多