Python词典列表,并根据特定情况创建另一个词典

时间:2018-10-30 05:15:29

标签: 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 '}
]

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:1)

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

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 ']]