Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 解析特殊字符之间的字符串并生成列表_Python_Python 3.x_Parsing - Fatal编程技术网

Python 解析特殊字符之间的字符串并生成列表

Python 解析特殊字符之间的字符串并生成列表,python,python-3.x,parsing,Python,Python 3.x,Parsing,我正在尝试制作一个脚本,该脚本将提供连接到我的网络的设备。这是用硒寻找元素的问题,但我找到了另一种方法。 现在,我得到了连接到我的wifi网络的所有设备的全部详细信息。看起来是这样的: @ @ pc/devicename1/ConnectedProtocol (usually 802.11)/Device mac address/DHCP/local ip adress/a random number that i don't want to use /another number that i

我正在尝试制作一个脚本,该脚本将提供连接到我的网络的设备。这是用硒寻找元素的问题,但我找到了另一种方法。 现在,我得到了连接到我的wifi网络的所有设备的全部详细信息。看起来是这样的:

@ @ pc/devicename1/ConnectedProtocol (usually 802.11)/Device mac address/DHCP/local ip adress/a random number that i don't want to use /another number that i don't want to use/ipv6 address/:ipv6::|pc/devicename1/ConnectedProtocol (usually 802.11)/Device mac address/DHCP/local ip adress/a random number that i don't want to use/another number that i don't want to use/ipv6 address/:ipv6 address::|@ 0@ 0|0|0|0@ 1@
@ @pc/android-d45cb2db4c2da/802.11/12:34:56:78:90:ab/DHCP/192.168.1.2/2/1/fe60::84c5:6de3:fe65:6bd8/:84c5:6de3:fe65:6bd8/::|pc/android-cd4b56add5/802.11/12:34:56:78:90:ab/DHCP/192.168.1.3/2/35/fe60::1547:bbb:fe69:8441/:2605:fff:fe97:9145/::@ 0@ 1|0|0|0@ 1@
因此,普通设备列表如下所示:

@ @ pc/devicename1/ConnectedProtocol (usually 802.11)/Device mac address/DHCP/local ip adress/a random number that i don't want to use /another number that i don't want to use/ipv6 address/:ipv6::|pc/devicename1/ConnectedProtocol (usually 802.11)/Device mac address/DHCP/local ip adress/a random number that i don't want to use/another number that i don't want to use/ipv6 address/:ipv6 address::|@ 0@ 0|0|0|0@ 1@
@ @pc/android-d45cb2db4c2da/802.11/12:34:56:78:90:ab/DHCP/192.168.1.2/2/1/fe60::84c5:6de3:fe65:6bd8/:84c5:6de3:fe65:6bd8/::|pc/android-cd4b56add5/802.11/12:34:56:78:90:ab/DHCP/192.168.1.3/2/35/fe60::1547:bbb:fe69:8441/:2605:fff:fe97:9145/::@ 0@ 1|0|0|0@ 1@
输出应如下所示:

@ @ pc/devicename1/ConnectedProtocol (usually 802.11)/Device mac address/DHCP/local ip adress/a random number that i don't want to use /another number that i don't want to use/ipv6 address/:ipv6::|pc/devicename1/ConnectedProtocol (usually 802.11)/Device mac address/DHCP/local ip adress/a random number that i don't want to use/another number that i don't want to use/ipv6 address/:ipv6 address::|@ 0@ 0|0|0|0@ 1@
@ @pc/android-d45cb2db4c2da/802.11/12:34:56:78:90:ab/DHCP/192.168.1.2/2/1/fe60::84c5:6de3:fe65:6bd8/:84c5:6de3:fe65:6bd8/::|pc/android-cd4b56add5/802.11/12:34:56:78:90:ab/DHCP/192.168.1.3/2/35/fe60::1547:bbb:fe69:8441/:2605:fff:fe97:9145/::@ 0@ 1|0|0|0@ 1@
  • 设备名称:android-d45cb2db4c2da
  • Mac地址:12:34:56:78:90:ab
  • 本地ip:192.168.1.2
  • 连接协议:Wlan
然后是另一个设备。
如何操作?

您可以将初始字符串拆分为包含每个设备信息的较小子字符串,然后再次拆分以获得所需信息:

devices = "@ @pc/android-d45cb2db4c2da/802.11/12:34:56:78:90:ab/DHCP/192.168.1.2/2/1/fe60::84c5:6de3:fe65:6bd8/:84c5:6de3:fe65:6bd8/::|pc/android-cd4b56add5/802.11/12:34:56:78:90:ab/DHCP/192.168.1.3/2/35/fe60::1547:bbb:fe69:8441/:2605:fff:fe97:9145/::@ 0@ 1|0|0|0@ 1"
devices = devices.split("::|")
for device in devices:
    device = device.split("/")
    print("Device name: " + device[1])
    print("MAC address: " + device[3])
    print("Local IP: " + device[5])
    print("Connected protocol: " + device[2])
    print()
输出:

Device name: android-d45cb2db4c2da
MAC address: 12:34:56:78:90:ab
Local IP: 192.168.1.2
Connected protocol: 802.11

Device name: android-cd4b56add5
MAC address: 12:34:56:78:90:ab
Local IP: 192.168.1.3
Connected protocol: 802.11
注意:为了确定您使用的是WLAN还是以太网或任何其他协议,最好创建一个字典,其中包含IEEE命名标准的键,例如
802.11
,以及协议类型的值,例如
WLAN