简体   繁体   English

如何在没有“SSID:”的情况下获取 python 中所有可用的 SSID 名称?

[英]How to get all available SSID names in python without “SSID:”?

Ok so my code is好的,所以我的代码是

results = subprocess.check_output(["netsh", "wlan", "show", "network"])
results = results.decode("ascii") # needed in python 3
results = results.replace("\r","")
ls = results.split("\n")
ls = ls[4:]
ssids = []
x = 0
while x < len(ls):
    if x % 5 == 0:
        ssids.append(ls[x])
    x += 1
print(ssids)

How could I get the output of the variable "ssids" without it saying "SSID (number of SSID): " before each item?我怎么能得到变量“ssids”的 output 而没有在每个项目之前说“SSID(SSID 数量):”?

Running this locally using Windows yielded a results list containing the following example elements:使用 Windows 在本地运行它会产生一个包含以下示例元素的results列表:

['SSID 1: WYZE_D73596CE021C547F', 'SSID 2: FiOS-ZLGT4-5G', ...]

Consequently, if you wanted to modify the list of SSIDs, you could do the following:因此,如果您想修改 SSID 列表,您可以执行以下操作:

ssids = [ssid.split(':',1)[-1].strip() for ssid in ssids]

This is a list comprehension which iterates over the existing SSIDs, splits once on a colon and takes the last element, and strips the resulting string (ie removes whitespace on either end), and will assign the new list to ssids:这是一个列表解析,它迭代现有的 SSID,在冒号上拆分一次并获取最后一个元素,并去除结果字符串(即删除两端的空格),并将新列表分配给 ssid:

['WYZE_D73596CE021C547F', 'FiOS-ZLGT4-5G', ...]

Alternatively, you could iterate over the existing list and print each element if you did not want to modify SSIDs:或者,如果您不想修改 SSID,可以遍历现有列表并打印每个元素:

for ssid in ssids:
    print(f'{ssid.split(":")[-1].strip()}')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM