python中字符串匹配的搜索部分

2024-06-01 00:44:46 发布

您现在位置:Python中文网/ 问答频道 /正文

我有两个文件,一个是“error_dict”文件,另一个是“jbosslogfile”,error dict文件在每行中都包含已知的错误消息,如 错误指令

"0500 ERROR [org.picketlink.identity.federation]"
"-0500 ERROR [com.gravitant.cloud.appsportfolio.jsf.architecture.beans.StorageAccountBean]"
"Invalid Context Code - APP"

jbosslog文件

2016-06-03 00:19:52,612 -0500 ERROR [org.jboss.as.ejb3.invocation] (EJB default - 3) JBAS014134: EJB Invocation failed on component GraBmsDataLoaderEjbIfc for method public abstract void com.gravitant.bms.common.dataload.GraBmsDataLoaderEjbIfc.loadVMTemplates(java.lang.String,java.lang.String,java.lang.String): javax.ejb.EJBException: com.gravitant.bts.bms.exception.GraException: Display Message:Transaction commit failed - 
2016-06-03 00:20:10,809 -0500 ERROR [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (EJB default - 4) Duplicate entry '9db562c525e942698c605df2f0c9b26a__FreeBSD10_3-2016-06-02::unreco' for key 'UQ_graresourcetemplate$tmpId_prv_usrGrpId'
2016-06-03 00:20:10,824 -0500 ERROR [com.gravitant.bts.bms.transaction.BTSTransaction] (EJB default - 4) BmsBaseTransaction:Commit()- Transaction commit failed 
2016-06-03 00:20:11,001 -0500 ERROR [org.jboss.as.ejb3.invocation] (EJB default - 4) JBAS014134: EJB Invocation failed on component GraBmsDataLoaderEjbIfc for method public abstract void com.gravitant.bms.common.dataload.GraBmsDataLoaderEjbIfc.loadVMTemplates(java.lang.String,java.lang.String,java.lang.String): javax.ejb.EJBException: com.gravitant.bts.bms.exception.GraException: Display Message:Transaction commit failed - 
2016-06-03 00:31:56,730 -0500 ERROR [org.picketlink.identity.federation] (http-/10.200.212.143:8081-10) PLFED000263: Service Provider could not handle the request.: java.lang.IllegalArgumentException: PLFED000132: No assertions in reply from IDP
2016-06-03 00:52:01,379 -0500 ERROR [org.picketlink.identity.federation] (http-/10.200.212.143:8081-1) PLFED000263: Service Provider could not handle the request.: java.lang.IllegalArgumentException: PLFED000132: No assertions in reply from IDP
2016-06-03 01:11:49,938 -0500 ERROR [org.picketlink.identity.federation] (http-/10.200.212.143:8081-10) PLFED000263: Service Provider could not handle the request.: java.lang.IllegalArgumentException: PLFED000132: No assertions in reply from IDP
2016-06-03 01:41:59,942 -0500 ERROR [org.picketlink.identity.federation] (http-/10.200.212.143:8081-1) PLFED000263: Service Provider could not handle the request.: java.lang.IllegalArgumentException: PLFED000132: No assertions in reply from IDP
2016-06-03 02:02:04,783 -0500 ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/].[Faces Servlet]] (http-/10.200.212.143:8081-1) JBWEB000236: Servlet.service() for servlet Faces Servlet threw exception: javax.faces.application.ViewExpiredException: viewId:/appsportfolio-main.jsf - View /appsportfolio-main.jsf could not be restored.
2016-06-03 02:11:57,211 -0500 ERROR [org.picketlink.identity.federation] (http-/10.200.212.143:8081-1) PLFED000263: Service Provider could not handle the request.: java.lang.IllegalArgumentException: PLFED000132: No assertions in reply from IDP
2016-06-03 02:22:02,739 -0500 ERROR [org.picketlink.identity.federation] (http-/10.200.212.143:8081-10) PLFED000263: Service Provider could not handle the request.: java.lang.IllegalArgumentException: PLFED000132: No assertions in reply from IDP
201

我尝试做的很简单,我使用这个错误dict文件,每个已知的错误匹配我的jboss日志文件,在这种情况下,如果我从jboss日志文件中找到字符串的一部分,在屏幕上打印该行,然后从jboss日志文件中拉出该行。你知道吗

非常类似我可以用notepad++实现,比如标记所有行,然后删除书签会执行yank操作。非常相似,我试图在python代码中实现,如下所示

def MatchCountYankerrors():
    ferrorrepo = errorrepopath
    conclufile = os.path.join(Decompressfilepath,(appservername+'conclusion'))
    ferrorfile = open(ferrorrepo)
    confile = open(conclufile)
    output = []

    for errlines in ferrorfile: #Pick each line from error_dict
        c = 0
        for eachconline in confile:#pick each line from Jboss log
            #if re.search(errlines,eachconline,re.M|re.I):
            newerrliens = errlines.strip().split() #error_dict file each line strip and spilit
            neweachconline = eachconline.strip().split() #Jbosslog file each line strip and spliting
            if newerrliens in neweachconline:
                print neweachconline

目标: 如何使用python程序执行以下操作 enter image description here


Tags: 文件infromorgcomhttplangnot
1条回答
网友
1楼 · 发布于 2024-06-01 00:44:46

下面是代码,循环通过我的错误repo文件,并在最终的错误日志文件,如果有匹配的行,它将从日志文件中拉(删除)这一行,并在执行此操作之前,它将创建备份文件。你知道吗

#This function will be matching error lines from error repo files and if matched it will yank those lines from the files.
def MatchandYankerrors():
    ferrorrepo = errorrepopath
    conclufile = os.path.join(Decompressfilepath,(appservername+'conclusion'))
    ferrorfile = open(ferrorrepo)
    output = []

    for errlines in ferrorfile: #Pick each line from error_dict
        c = 0
        newerrliens = errlines.strip()  # error_dict file each line strip and spilit
        #confile = open(conclufile,"r+")#This will keep opening file every time when we need new error to search.
        #confilelines = confile.readlines() #This will read all lines from file.
        #confile.seek(0)
        i=0
        for line in fileinput.input(conclufile,inplace=1,backup='.orig'):
            line = line.strip()
            if newerrliens in line:
                pass
            else:
                print line
        fileinput.close()

相关问题 更多 >