Python 提取日志文件中的数据部分

Python 提取日志文件中的数据部分,python,pandas,Python,Pandas,我正在分析日志文件中的数据。我的日志文件如下所示: [2018-07-13 03:04:57] production.DEBUG: No problem MemId: 000MemId or CardNo There is no staff information MemId: 2956144 without the bird - file; mbs There is no staff information There is no staff information [2018-07-13

我正在分析日志文件中的数据。我的日志文件如下所示:

[2018-07-13 03:04:57] production.DEBUG: No problem MemId: 000MemId or CardNo 
There is no staff information
MemId: 2956144 without the bird - file; mbs
There is no staff information
There is no staff information
[2018-07-13 03:06:07] production.DEBUG: No problem MemId: 00mem_id or CardNo
我想在pandas中创建一个数据帧。我的预期结果:

TimeStand           Screen           Level         messenger
2018-07-13 03:04:57 production  DEBUG    No problem MemId...staff information
2018-07-13 03:06:07 production  DEBUG    No problem MemId:  00mem_id or CardNo
像这样:

[2018-07-13 03:04:57] production.DEBUG: No problem MemId: 000MemId or CardNo 
There is no staff information
MemId: 2956144 without the bird - file; mbs
There is no staff information
There is no staff information
[2018-07-13 03:06:07] production.DEBUG: No problem MemId: 00mem_id or CardNo


我曾想过使用正则表达式,但我是Python的初学者。

我建议开始阅读代码:

import pandas as pd
TimeStand, Screen, Level, messenger = []
log = open('log.txt', 'r')
for line in log:
    if ....:
         TimeStand.append(...)
    elif ....:
         Screen.append(...)
 df = pd.DataFrame({'TimeStand': TimeStand, 'Screen': Screen, 'Level': Level, 'messenger': messenger

我已经编写了代码,您没有指定太多,但是您将了解如何使用正则表达式,并且能够对其进行操作。 还可以通过谷歌搜索str.strip来剥离一些字符

import re
import pandas as pd



st= '[2018-07-13 03:04:57] production.DEBUG: No problem MemId: 000MemId or CardNo There is no staff information MemId: 2956144 without the bird - file; mbs There is no staff information here is no staff information [2018-07-13 03:06:07] production.DEBUG: No problem MemId: 00mem_id or CardNo [2018...etc]'


timelist=re.findall('\[\w\S*\s\w*\S*]',st)
df=pd.DataFrame({'TimeStand': timelist})
screenlist=re.findall(r'\bproduction\b',st)
df['Screen']=screenlist
levellist=re.findall(r'\bDEBUG\b',st)
df['Level']=levellist
messengerlist=re.findall(r'\: .*?\[',st)
df['Messenger']=messengerlist
输出如下所示-

TimeStand      Screen  Level  \
0  [2018-07-13 03:04:57]  production  DEBUG   
1  [2018-07-13 03:06:07]  production  DEBUG   

                                           Messenger  
0  : No problem MemId: 000MemId or CardNo There i...  
1           : No problem MemId: 00mem_id or CardNo [  
我有这个解决办法。 我的代码是:

import pandas as pd
import json
import pyes # For documentation around pyes.es : https://pyes.readthedocs.org/en/latest/references/pyes.es.html
import requests
import  numpy as np
import datetime
import inspect
import re


v = open(r"C:/laravel-2019-06-01.log","r",encoding='utf-8-sig')


st = v.read()

st = st + '[2018-07-14]'

st = st.replace('\n',' ')

timelist=re.findall('\d{4}[-/]\d{2}[-/]\d{2} \d{2}[:]\d{2}[:]\d{2}',st)
df=pd.DataFrame({'TimeStand': timelist})
screenlist=re.findall(r'\d{2}[:]\d{2}[:]\d{2}\].*?\.',st)
df['TimeStand'] = df['TimeStand'].str.strip('][')
df['Screen']=screenlist
df['Screen'] = df['Screen'].map(lambda x: str(x)[10:])
df['Screen'] = df['Screen'].map(lambda x: str(x)[:-1])
levellist=re.findall(r'\d{2}[:]\d{2}[:]\d{2}\].*?\..*?\:',st)
df['Level']=levellist
df['Level'] = df['Level'].map(lambda x: str(x)[21:])
df['Level'] = df['Level'].map(lambda x: str(x)[:-1])
messengerlist=re.findall(r'\d{2}[:]\d{2}[:]\d{2}\].*?\..*?\: .*?\[\d{4}[-/]\d{2}[-/]\d{2}',st)
df['Messenger']=messengerlist
df['Messenger']  = np.where(df['Level']=='ERROR',df['Messenger'].map(lambda x: str(x)[27:]),np.where(df['Level']=='DEBUG',df['Messenger'].map(lambda x: str(x)[27:]),np.where(df['Level']=='CRITICAL',df['Messenger'].map(lambda x: str(x)[30:]),np.where(df['Level']=='ALERT',df['Messenger'].map(lambda x: str(x)[27:]),np.where(df['Level']=='NOTICE',df['Messenger'].map(lambda x: str(x)[28:]),np.where(df['Level']=='INFO',df['Messenger'].map(lambda x: str(x)[26:]),np.where(df['Level']=='WARNING',df['Messenger'].map(lambda x: str(x)[29:]),df['Messenger'].map(lambda x: str(x)[31:]))))))))

df['Messenger']  = df['Messenger'].map(lambda x: str(x)[:-11])

print(df)
我希望这一解决办法将帮助那些需要它的人。
非常感谢

请向我们提供您当前正在处理的代码。您好,在您的文件之后,我需要添加“[”。所以这不是我说的,请使用str.strip删除[和:在这些列中,您已经设置好了。嗨,如果屏幕的值不仅仅是生产,它可能是另一个值。我正在尝试为这部分编写正则表达式,但我还没有完成