一个6000行的TXT,执行起来要10多分钟,帮忙优化下
TXT文档里面的内容如下
interface Eth-Trunk152.508
vlan-type dot1q 508
description TO-[S-FW-02]-Eth-Trunk3.508-2*100
ip binding vpn-instance VPN
ipv6 enable
ip address 192.168.5.49 255.255.255.252
ipv6 address 1:1:8617:182::4/127
ospfv3 158 area 0.0.0.0
ospfv3 network-type p2p
ospfv3 authentication-mode hmac-sha256 key-id 1 cipher %^%#;9`I!S|tj;D~S|$04jB;n'h=%H}M%V$0x[3L7f-:%^%#
ospf authentication-mode md5 1 cipher %^%#wxyCG2(f=2TlhwFsM\J9kN'T49C"J/LLYbGAqH}S%^%#
ospf network-type p2p
interface和ip bindding vpn-in 之间的行数不确定,不超过4个,ip address和ip bindding vpn-in 之间的行数不确定,不超过4个
还有很多不是这样的信息
为啥这么吗? 是哪里出了问题
```python
import re
import copy
import pandas as pd
import os
with open('汇总.txt', 'r', encoding='utf-8', errors='ignore') as file:
f=file.read()
IPV4_P_T = (r'interface (\w\S+\d)\n',
r'.*\n?',
r'.*\n?',
r'.*\n?',
r'.*\n?',
r' ip binding vpn-instance (\S+)\n'
r'.*\n?',
r'.*\n?',
r'.*\n?',
r'.*\n?',
r' ip address (\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3} \d{1,3}.\d{1,3}.\d\d{1,3}.{1,4})'
)
IPV4_P="".join(IPV4_P_T)
IPV4=re.findall(IPV4_P,f)
IPV6_P_T = (r'interface (\w\S+\d)\n',
r'.*\n?',
r'.*\n?',
r'.*\n?',
r'.*\n?',
r' ip binding vpn-instance (\S+)\n'
r'.*\n?',
r'.*\n?',
r'.*\n?',
r'.*\n?',
r' ipv6 address (\S+/\d{1,3})'
)
IPV6_P="".join(IPV6_P_T)
IPV6=re.findall(IPV6_P,f)
print(IPV6)
one={}
IPV4_ALL=[]
IPV6_ALL=[]
for i in IPV4:
one['inteface']=i[0]
one['VPN']=i[1]
one['IPV4']=i[2]
IPV4_ALL.append(copy.deepcopy(one))
one={}
for i in IPV6:
one['inteface']=i[0]
one['VPN']=i[1]
one['IPV6']=i[2]
IPV6_ALL.append(copy.deepcopy(one))
one={}
df_IPV4 = pd.DataFrame(IPV4_ALL)
df_IPV4.to_excel('汇总.xlsx', sheet_name='IPV4',index=False)
df_IPV6 = pd.DataFrame(IPV6_ALL)
with pd.ExcelWriter('汇总.xlsx', mode='a') as writer:
df_IPV6.to_excel(writer, sheet_name='IPV6', index=False)
```