本站消息

站长简介/公众号


站长简介:高级软件工程师,曾在阿里云,每日优鲜从事全栈开发工作,利用周末时间开发出本站,欢迎关注我的微信公众号:程序员总部,程序员的家,探索程序员的人生之路!分享IT最新技术,关注行业最新动向,让你永不落伍。了解同行们的工资,生活工作中的酸甜苦辣,谋求程序员的最终出路!

  价值13000svip视频教程,python大神匠心打造,零基础python开发工程师视频教程全套,基础+进阶+项目实战,包含课件和源码

  出租广告位,需要合作请联系站长

+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

暂无数据

读取日志文件并在 1 行中转储 json

发布于2021-12-02 03:20     阅读(560)     评论(0)     点赞(4)     收藏(1)



我有一个看起来像这样的日志文件:

>>> 2017-08-02 08:51:45 +0200 [INFO] from com.sun.metro.assembler in application-siaServiceImplPort-context-362552 - MASM0007: No application metro.xml configuration file found.
>>> 2017-08-02 08:53:06 +0200 [INFO] from application in application-akka.actor.default-dispatcher-362046 - LOG_EVENT: {
  "event" : "sxxxxxdd",
  "ts" : "2017-xx
  "svc" : "dxx.tlc-1",
  "rexxxt" : {
    "ts" : "2017-xxxx2:00",
    "xx" : "73478c0f-dc70-46b7-a388-d12f7b8aa91e",
    "xxxx" : "/xxx/xxx",
    "xxx" : "POST",
    "user_agent" : "xxx/6.2.1 xxxx/7.38.0 xxx/7.0xx16-1~xxx+8.1",
    "user_id" : 39,
    "xxx_ip" : "xxxx.1",
    "xxxx" : "xxxxx",
    "xx" : "xx",
    "app_id" : "d4da4385a8204be2949ed62323231443",
    "axxe" : "POxxkout"
  },
  "operation" : {
    "scxe" : "checkout",
    "rxxxlt" : {
      "xxxus" : 2x0
    }
  },
  "xx" : {
    "xxx_id" : "CHTO06MLKXP9N",
    "xxx_attributes" : {
      "xx" : "2017xx6+02:00",
      "date_xxxxx" : "2xx7-08xx53:06+02:00",
      "xus" : "WAxING",
      "dexxion" : "numx0",
      "chaxxmount" : 2,
      "chaxx_start" : "20x8xx+02:00",
      "charge_max_count" : 1,
      "merchant" : {
        "xxx" : "xxxx",
        "xxx" : "xxxxxxx",
        "xx" : "xx-x xxxxxl.",
        "logo" : "httxxxff0/258xxxjpeg",
        "account_type" : "B"
      },
      "xx_xxx" : "xxxx",
      "xxxx_xxx_url" : "https://xxx.xxx.xxx-pay.xx/xxx",
      "xxx" : "xxxx",
      "xxx" : "xx://dp.xx/uxx10/xxxx"
    }
  },
  "cxx" : "xxxx"
}

该文件当然有更多行,替代单行,如示例和具有多行 json 的行。

我想要实现的是有一个读取文件的文件,每当它出现一行包含 json 时,自动转储到 1 行。

所以它会像:

>>> 2017-08-02 08:51:45 +0200 [INFO] from com.sun.metro.assembler in application-siaServiceImplPort-context-362552 - MASM0007: No application metro.xml configuration file found.
>>> 2017-08-02 08:53:06 +0200 [INFO] from application in application-akka.actor.default-dispatcher-362046 - LOG_EVENT: {the json here in 1 line}

我试过,使用 python,如下:

infile = "/hoxxxx/application.log"

重要 = [] keep_phrases = "LOG_EVENT"

with open(infile) as f:
    f = f.readlines()

for line in f:
    if keep_phrases in line:
        print(line)
        important.append(line)

但这正在返回该行,但当然它不明白 json 在哪里结束......

有什么帮助吗?谢谢


解决方案


你可以尝试使用正则表达式和 json

正则表达式:

import re

with open(infile) as f:
    text = f.read()

print re.sub(r'\n([^>])', r'\1', text)

输出:

>>> 2017-08-02 08:51:45 +0200 [INFO] from com.sun.metro.assembler in application-siaServiceImplPort-context-362552 - MASM0007: No application metro.xml configuration file found.
>>> 2017-08-02 08:53:06 +0200 [INFO] from application in application-akka.actor.default-dispatcher-362046 - LOG_EVENT: {"event" : "sxxxxxdd","ts" : "2017-xx"svc" : "dxx.tlc-1","rexxxt" : {"ts" : "2017-xxxx2:00","xx" : "73478c0f-dc70-46b7-a388-d12f7b8aa91e","xxxx" : "/xxx/xxx","xxx" : "POST","user_agent" : "xxx/6.2.1 xxxx/7.38.0 xxx/7.0xx16-1~xxx+8.1","user_id" : 39,"xxx_ip" : "xxxx.1","xxxx" : "xxxxx","xx" : "xx","app_id" : "d4da4385a8204be2949ed62323231443","axxe" : "POxxkout"},"operation" : {"scxe" : "checkout","rxxxlt" : {"xxxus" : 2x0}},"xx" : {"xxx_id" : "CHTO06MLKXP9N","xxx_attributes" : {"xx" : "2017xx6+02:00","date_xxxxx" : "2xx7-08xx53:06+02:00","xus" : "WAxING","dexxion" : "numx0","chaxxmount" : 2,"chaxx_start" : "20x8xx+02:00","charge_max_count" : 1,"merchant" : {"xxx" : "xxxx","xxx" : "xxxxxxx","xx" : "xx-x xxxxxl.","logo" : "httxxxff0/258xxxjpeg","account_type" : "B"},"xx_xxx" : "xxxx","xxxx_xxx_url" : "https://xxx.xxx.xxx-pay.xx/xxx","xxx" : "xxxx","xxx" : "xx://dp.xx/uxx10/xxxx"}},"cxx" : "xxxx"}

如果你想要 jsons 作为 python 对象,你也可以这样做:

import json

text2 = re.sub(r'\n([^>])', r'\1', text)
js = [json.loads(x) for x in re.findall(r'{.*}', text2)]






所属网站分类: 技术文章 > 问答

作者:黑洞官方问答小能手

链接:https://www.pythonheidong.com/blog/article/1107008/0a88bd652640cd96d0c0/

来源:python黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

4 0
收藏该文
已收藏

评论内容:(最多支持255个字符)