首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >解析日志并存储Dict

解析日志并存储Dict
EN

Stack Overflow用户
提问于 2018-04-06 19:55:29
回答 2查看 1.2K关注 0票数 0

BGP日志文件: bgplog.log

代码语言:javascript
运行
复制
Host local.domain.net [11.130.55.2] with interface to BGP peer 11.130.44.65: the number of BGP UPDATE messages received changed from '110376' to '110393'
Host local.domain.net [11.130.55.2] with interface to BGP peer 11.130.44.93: the number of BGP UPDATE messages received changed from '133736' to '134146'
Host local.domain.net [11.130.55.2] with interface to BGP peer 11.130.44.65: the number of BGP UPDATE messages sent changed from '108252' to '108348'
Host local.domain.net [11.130.55.2] with interface to BGP peer 11.130.44.93: the number of BGP UPDATE messages sent changed from '2094' to '2132'
Host local.domain.net [11.130.55.2] with interface to BGP peer 11.130.44.103: the number of BGP UPDATE messages sent changed from '91440' to '91462'
Host local.domain.net [11.130.55.2] with interface to BGP peer eth1-local.domain.net [11.8.44.10]: the number of BGP UPDATE messages sent changed from '1411' to '1413'
Host local.domain.net [11.130.55.2] with interface to BGP peer 10.81.244.18: the number of BGP UPDATE messages sent changed from '112347' to '112506'
Host local.domain.net [11.130.55.2] with interface to BGP peer 11.130.44.65: the number of messages received from the remote peer changed from '538672' to '538691'
Host local.domain.net [11.130.55.2] with interface to BGP peer 11.130.44.93: the number of messages received from the remote peer changed from '547397' to '547814'

目标:

  1. 找出具体的IP,比方说: 11.130.44.93
  2. 任何行都与上面的IP匹配,拆分行并将某些值存储到每个键中。
  3. 按密钥排序

下面是我尝试过的代码:似乎我被卡住了

代码语言:javascript
运行
复制
import re
import os


def find(line):
    findThis = ""
    found = re.match(r'.*?11.130.44.103.*', line)
    # Find is true:
    if found:
        # found a item and adds in to findThis
        findThis = found.group()
    else:
        findThis = "NONE"
    return findThis


def generateDicts(log):
    currentDict = {}
    for line in log:
        if line.startswith(find(line)):
            currentDict = {
                "host": line.split(" ")[1][:24],
                "ip": line.split(" ", 9)[2],
                "peer": line.split(" ")[8],
                "info": line.split(" ", 9)[-1]}
        else:
            # currentDict = {"info":line.split("-",6)[-1]}
            currentDict = line


with open("bgplog.txt") as f:
    print list(generateDicts(f))

我只得到最后一个值,不知怎么的,它不是附加的。什么是最好的方法?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-04-06 20:27:58

我猜您需要一个生成器,在这种情况下,您需要使用yield关键字。试试这个:

代码语言:javascript
运行
复制
def generateDicts(log):
    for line in log:
        if line.startswith(find(line)):
            yield {
                "host": line.split(" ")[1][:24],
                "ip": line.split(" ", 9)[2],
                "peer": line.split(" ")[8],
                "info": line.split(" ", 9)[-1]}
票数 0
EN

Stack Overflow用户

发布于 2018-04-06 20:48:01

一种方法可以是在列表中附加这个数据集。不使用regex如果没有必要..。

代码语言:javascript
运行
复制
with open("bgplog.txt") as log:
    ip_to_search = '11.130.55.2'
    result = []

    for log_line in log:
        if ip_to_search in log_line:
            currentDict = {
                "host": log_line.split(" ")[1][:24],
                "ip": log_line.split(" ", 9)[2],
                "peer": log_line.split(" ")[8],
                "info": log_line.split(" ", 9)[-1]}
            result.append(currentDict)

    for element in result:
        print(element)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49700164

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档