Python字典列表,并从特定ca创建另一个字典

2024-09-29 19:20:36 发布

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

我有一份字典清单。你知道吗

    data = [
    {'@timestamp': '2018-10-29T05:57:12.722Z','messages': '[FANATIC] - - Session id kajdhrg7uhdfvbshfgadf '},
    {'@timestamp': '2018-10-29T06:58:12.722Z','messages': '[FANATIC] - - Exception Lorem Ipsum Rebolt '},
    {'@timestamp': '2018-10-29T06:58:12.722Z','messages': 'JHUSHDVCHBASJd'},
    {'@timestamp': '2018-10-29T06:58:12.722Z','messages': 'asdfawerg cdv '},
    {'@timestamp': '2018-10-29T06:58:12.722Z','messages': 'fya7 5 Lorem Ipsum Rebolt '},
    {'@timestamp': '2018-10-29T06:58:12.722Z','messages': 'zxcgwrt asdfg w4e6354gdf '},
    {'@timestamp': '2018-10-29T06:58:12.722Z','messages': 'zcfb26 eqrgsfdb syh2456ytdfg '},
    {'@timestamp': '2018-10-29T06:58:12.722Z','messages': 'we57hb354gf '},
    {'@timestamp': '2018-10-29T06:59:12.722Z','messages': '[FANATIC] - - sdfsum Rebolt '},
    {'@timestamp': '2018-10-29T06:59:12.722Z','messages': '[FANATIC] - - sdfsum Rebolt '}
    ]

这是一个日志模式。但是错误日志新行没有[FANATIC],它应该在单行或单个消息中。你知道吗

有没有办法让那些在新的字典里只包含错误的共轭信息?你知道吗

for i in data:
    if "[FANATIC]" in i['messages'] and "exception" in i['messages']:
        print(i)

我能在新字典里找到“行间”吗?你知道吗

编辑:

我想要的类似于下面的…:

data = [
{'@timestamp': '2018-10-29T06:58:12.722Z','messages': '[FANATIC] - - Exception Lorem Ipsum Rebolt '},
{'@timestamp': '2018-10-29T06:58:12.722Z','messages': 'JHUSHDVCHBASJd'},
{'@timestamp': '2018-10-29T06:58:12.722Z','messages': 'asdfawerg cdv '},
{'@timestamp': '2018-10-29T06:58:12.722Z','messages': 'fya7 5 Lorem Ipsum Rebolt '},
{'@timestamp': '2018-10-29T06:58:12.722Z','messages': 'zxcgwrt asdfg w4e6354gdf '},
{'@timestamp': '2018-10-29T06:58:12.722Z','messages': 'zcfb26 eqrgsfdb syh2456ytdfg '},
{'@timestamp': '2018-10-29T06:58:12.722Z','messages': 'we57hb354gf '},
{'@timestamp': '2018-10-29T06:58:12.722Z', 'messages': [
'[FANATIC] - - Exception Lorem Ipsum Rebolt', 
'JHUSHDVCHBASJd', 
'asdfawerg cdv ', 
'fya7 5 Lorem Ipsum Rebolt ',
'zxcgwrt asdfg w4e6354gdf ',
'we57hb354gf '
]
},
{'@timestamp': '2018-10-29T06:58:12.722Z','messages': '[FANATIC] - - sdfsum Rebolt '},
{'@timestamp': '2018-10-29T06:58:12.722Z','messages': '[FANATIC] - - sdfsum Rebolt '}
]

Tags: data字典exceptiontimestampmessagesipsumloremcdv
2条回答

只需用理解力过滤你的列表:

res = [x for x in data if x["messages"].startswith("[FANATIC]")]

或者对于这两种检查:

res2 = [x for x in data if all(s in x["messages"] for s in ("Exception", "FANATIC"))]

为了得到“线之间”的东西,你可以从左到右剥离:

def StriptStuff(s):
    return s.lstrip("[FANATIC] -").rstrip("- Exception Lorem Ipsum Rebolt ")

res2 = [StriptStuff(x["messages"]) for x in data if all(s in x["messages"] for s in ("Exception", "FANATIC"))]

但对于更复杂的东西,regex会更好。你知道吗

这里有一个live example

我将异常添加到一个中间列表中,然后将该列表存储在另一个列表中

from pprint import pprint
data = [
    {'@timestamp': '2018-10-29T05:57:12.722Z','messages': '[FANATIC] - - Session id kajdhrg7uhdfvbshfgadf '},
    {'@timestamp': '2018-10-29T06:58:12.722Z','messages': '[FANATIC] - - Exception Lorem Ipsum Rebolt '},
    {'@timestamp': '2018-10-29T06:58:12.722Z','messages': 'JHUSHDVCHBASJd'},
    {'@timestamp': '2018-10-29T06:58:12.722Z','messages': 'asdfawerg cdv '},
    {'@timestamp': '2018-10-29T06:58:12.722Z','messages': 'fya7 5 Lorem Ipsum Rebolt '},
    {'@timestamp': '2018-10-29T06:58:12.722Z','messages': 'zxcgwrt asdfg w4e6354gdf '},
    {'@timestamp': '2018-10-29T06:58:12.722Z','messages': 'zcfb26 eqrgsfdb syh2456ytdfg '},
    {'@timestamp': '2018-10-29T06:58:12.722Z','messages': 'we57hb354gf '},
    {'@timestamp': '2018-10-29T06:59:12.722Z','messages': '[FANATIC] - - Exception Lorem Ipsum Rebolt '},
    {'@timestamp': '2018-10-29T06:59:12.722Z','messages': '[FANATIC] - - Exception Lorem Ipsum Rebolt '}
]
counter=0
exceptionlist=[]
while(counter<len(data)):
    if "[FANATIC]" in data[counter]['messages']:
        oneexception=[ data[counter]['messages']]
        counter+=1
        while (counter<len(data) and "[FANATIC]" not in data[counter]['messages'] ):
            oneexception.append( data[counter]['messages'])
            counter+=1
        exceptionlist.append( oneexception)
pprint(exceptionlist)

输出

[['[FANATIC] - - Session id kajdhrg7uhdfvbshfgadf '],
 ['[FANATIC] - - Exception Lorem Ipsum Rebolt ',
  'JHUSHDVCHBASJd',
  'asdfawerg cdv ',
  'fya7 5 Lorem Ipsum Rebolt ',
  'zxcgwrt asdfg w4e6354gdf ',
  'zcfb26 eqrgsfdb syh2456ytdfg ',
  'we57hb354gf '],
 ['[FANATIC] - - Exception Lorem Ipsum Rebolt '],
 ['[FANATIC] - - Exception Lorem Ipsum Rebolt ']]

相关问题 更多 >

    热门问题