python删除某一行字符前面的内容

2024-11-02 17:28:04
推荐回答(2个)
回答1:

import re

a='''Aug 24 2018 10:29:59 SRM_ENTITY 4 CARDINVALIDRESUI4E:OID 1.3.6.1.4.1.2011.5.25.219.2.3.6 The card recovered from total failure.

Aug 24 2018 10:30:00 IFNET 1 IF_LINKDOWN:OID 1.3.6.1.6.3.1.1.5.3 Interface 7 turned into DOWN state.

Aug 24 2018 10:30:00 IFNET 4 IF_CARD_CFI4OVER:OID 1.3.6.1.4.1.2011.5.25.41.3.27 The configuration of the interface card is restored.

Aug 24 2018 10:30:47 HWCM 4 CFGCHANGE:OID 1.3.6.1.4.1.2011.6.10.2.1 Configure changed.

Aug 24 2018 10:30:47 LINE 5 VTYUSERLOGOUT:OID 1.3.6.1.4.1.2011.5.25.207.2.4 A user logout.

Aug 24 2018 10:36:31 INFO 4 IC_LOGFILE_AGING:OID 1.3.6.1.4.1.2011.5.25.212.2.2 One log file is deleted due to aging.

Aug 24 2018 10:38:52 CGN 4 CGN2USER_PORT_NUI4_TRAP:Sloc=7,Vcpu=0:OID 1.3.6.1.4.1.2011.5.25.240.2.4 The number of pores assigned to Cl

Aug 24 2018 10:56:52 LINE 5 VTYUSERLOGIn7:OID 1.3.6.1.4.1.2011.5.25.207.2.2 A user login.

Aug 24 2018 10:56:58 LINE 5 VTYUSERLOGOUT:OID 1.3.6.1.4.1.2011.5.25.207.2.4 A user logout.

Aug 24 2018 11:07:30 SNMP 4 AUTHFAIL:OID 1.3.6.1.6.3.1.1.5.5 authenticationFailure.'''

arr=a.split('\n')

[' '.join(x.split(' ')[:3]+["OID"]+x.split(' ')[7:8]) for x in arr]

回答2:

你这个我猜的不错的是关于股票或者那种网站爬取的信息吧,非常眼熟,你这需求不要太简单
import re
with open('a.txt','r',ecoding = 'utf8') as fr,open('new_a.text','a',ecoding ='utf8') as fa:
(缩进)for a in fr:
(缩进)(缩进)s = re.findall('(\S+?\s\S+?\s\S+?\s\S+?\s).*?:(OID \S+)',s)[0]

(缩进)(缩进)fa.write(s)