Converting log file to json?

前端 未结 2 863
清歌不尽
清歌不尽 2021-01-25 05:01

I have the below log file in the following format. I need to convert the log file into json file using python. How can it be made?

[2015-07-13 00:03:05,976] hostna

2条回答
  •  佛祖请我去吃肉
    2021-01-25 05:39

    Import pythons json library:

    import json
    

    Read in the file as a string and get everything after the 'Response:' substring:

    with open("logfile", "r") as log_file:
        log_string = log_file.read()
    response_string = log_string.split("Response:")[1].strip()
    

    Get a python object from response_string:

    response_obj = json.loads(response_string)
    

    If you need to, write that object out to a file after doing whatever you need with it:

    with open("outfile", "w") as out_file:
        out_file.write(json.dumps(response_obj))
    

提交回复
热议问题