【发布时间】: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