1、在字符串之间存在多个空格的时候,不按照一个空格来分隔,而是按照空白来分隔。
例如:
# IP Address MAC Address VLAN ID Interface Aging Type
arp = '''172.16.10.253 0050-56ab-64fb 20 GE1/0/46 20 D
172.16.10.250 0050-56ab-4460 20 BAGG8 6 D
172.16.10.251 f4e5-f2e9-d436 20 BAGG120 13 D
172.16.10.220 300d-9ec5-1c02 20 BAGG120 10 D
172.16.10.241 e443-4be6-7f50 20 BAGG120 20 D
172.16.10.242 e443-4be3-2dd0 20 BAGG120 19 D
172.16.10.243 e443-4be3-2f10 20 BAGG120 20 D
172.16.10.245 0050-56ab-cfb5 20 BAGG120 13 D
172.16.10.244 0050-56ab-450b 20 BAGG120 19 D'''
arp = arp.split('\n')
arp1 = arp[0].split(' ')
print(arp1)
输出结果如下:
['172.16.10.253', '', '', '', '0050-56ab-64fb', '', '20', '', '', '', '', '', '', 'GE1/0/46', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '20', '', '', '', 'D']
2、正确的做法是:
import re
arp1 = re.split(r"[ ]+", arp[0])
print(arp1)
输出结果如下:
['172.16.10.253', '0050-56ab-64fb', '20', 'GE1/0/46', '20', 'D']
文章评论