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

Converting the output of subprocess.check_output() into a dictionary

我是 运行 来自 Python 的命令 netsh wlan show interfaces 使用 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

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

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

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

如果需要将其转换为字典,请像这样使用re.split() with some

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

I am not getting what is the real problem, its with the string or something else?

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

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

关键是例如"Physical address" 和值 "c0:38:96:92:b7:1d"

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

The Netsh commands for the Windows Filtering Platform (WFP) enable you The tool then exports the collected data into an XML file that you can examine for clues to the cause of the problem.

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

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

这应该会为您提供一个计算机可读文件,可以使用 Python numerous XML tools 轻松遍历。

从对列表中形成字典

ValueError: dictionary update sequence element #0 has length 1; 2 is required

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

例如

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