将subprocess.check_output()的输出转换为字典

时间:2015-12-25 05:15:39

标签: python string python-2.7 dictionary subprocess

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

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

我没有得到真正的问题,是字符串还是别的什么?

2 个答案:

答案 0 :(得分:4)

  

我没有得到什么是真正的问题,它与字符串或其他什么?

您不能将字符串转换为字典,即使该字符串对人类也是如此。

你将不得不解析该字符串并从中获取(键,值)对。

关键是例如“物理地址”和值“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轻松遍历。

从成对列表中形成dicts

  

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

这是因为dict期望一个(键,值)对的列表,从中形成一个dict。 (键,值)的长度为2(它是一对)

e.g。

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

答案 1 :(得分:1)

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

>>> 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'