gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 21:48:54 25 4
gpt4 key购买 nike

我正在使用 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

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

最佳答案

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

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

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

关键是例如“物理地址”和值“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}

关于python - 将 subprocess.check_output() 的输出转换为字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34460199/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com