【问题标题】:converting txt data (from log file) into json using python?使用python将txt数据(来自日志文件)转换为json?
【发布时间】:2021-04-13 17:19:22
【问题描述】:

我有一个日志文件,格式如下:

parsed: {'priority': '14', 'timestamp': '2021-04-13 13:42:07', 'hostname': 'invi-dev-gw2', 'rootname': 'root', 'pid': '27889', 'message': 'Session STARTED - Client[ID:8242, Physical: 111.119.187.47, Virtual: 10.1.0.66] <--> Service[Name:Attendance1, ID:704, Physical: 192.168.3.18, Virtual: 10.1.0.67]'}
parsed: {'priority': '15', 'timestamp': '2021-04-13 13:42:07', 'hostname': 'invi-dev-gw3', 'rootname': 'root', 'pid': '27890', 'message': 'Session STOPPED - Client[ID:8242, Physical: 111.119.187.47, Virtual: 10.1.0.66] <--> Service[Name:Attendance1, ID:704, Physical: 192.168.3.18, Virtual: 10.1.0.67]'}

文本文件中基本上有两个数据。下一步是使用 Python 将文本数据转换为 JSON。到目前为止,我已经有了 JSON 转换的 python 脚本,如下所示:

# Python program to convert text 
# file to JSON 
import json 


# the file to be converted to 
# json format 
filename = 'output.txt'

# dictionary where the lines from 
# text will be stored 
dict1 = {} 

# creating dictionary 
with open(filename) as fh: 

    for line in fh: 

        # reads each line and trims of extra the spaces 
        # and gives only the valid words 
        command, description = line.strip().split(None, 1) 

        dict1[command] = description.strip() 

# creating json file 
# the JSON file is named as test1 
out_file = open("test.json", "w") 
json.dump(dict1, out_file, indent = 4, sort_keys = False) 
out_file.close() 

现在 JSON 文件已创建,但它只显示了一个数据(输出应该显示 2 个数据),如下所示:

    "parsed:": "{'priority': '15', 
                 'timestamp': '2021-04-13 13:42:07',
                  'hostname': 'invi-dev-gw3',
                  'rootname': 'root', 
                  'pid': '27890', 
                  'message': 'Session STOPPED - Client[ID:8242, Physical: 111.119.187.47, Virtual: 10.1.0.66] <--> Service[Name:Attendance1, ID:704, Physical: 192.168.3.18, Virtual: 10.1.0.67]'
}"

我不知道为什么它没有打印整个数据。它应该在 JSON 文件中显示另一个数据,但只显示一个。谁能帮我解决这个问题?

【问题讨论】:

  • 您已经将相同的parsed: 键两次放入字典中,第二次覆盖了第一次。而是建立一个列表,可能会丢弃过程中的parsed: 部分并仅保留描述。考虑显示您期望的输出。

标签: python json python-3.x logfile python-jsons


【解决方案1】:

如果你有dict1[command] = description.strip(),你真的想要更像:dict1[command].append(description.strip())(所以dict1 真的想成为一个列表)。还有一个问题是您可能想使用json.loads(),但您的输入数据使用单引号,所以让我们用ast 解析它

我会尝试类似:

import ast
import collections
import json

data_in = [
    "parsed: {'priority': '14', 'timestamp': '2021-04-13 13:42:07', 'hostname': 'invi-dev-gw2', 'rootname': 'root', 'pid': '27889', 'message': 'Session STARTED - Client[ID:8242, Physical: 111.119.187.47, Virtual: 10.1.0.66] <--> Service[Name:Attendance1, ID:704, Physical: 192.168.3.18, Virtual: 10.1.0.67]'}",
    "parsed: {'priority': '15', 'timestamp': '2021-04-13 13:42:07', 'hostname': 'invi-dev-gw3', 'rootname': 'root', 'pid': '27890', 'message': 'Session STOPPED - Client[ID:8242, Physical: 111.119.187.47, Virtual: 10.1.0.66] <--> Service[Name:Attendance1, ID:704, Physical: 192.168.3.18, Virtual: 10.1.0.67]'}"
]

data_out = collections.defaultdict(list)
for row in data_in:
    command, command_text = [value.strip() for value in row.split(":", 1)]
    data_out[command].append(ast.literal_eval(command_text))

print(json.dumps(data_out, indent=2))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-04-10
    • 2021-09-24
    • 1970-01-01
    • 2022-01-23
    • 1970-01-01
    • 2022-11-11
    • 2017-02-09
    • 2021-12-20
    相关资源
    最近更新 更多