【问题标题】:How can i convert this log data into JSON using a python script如何使用 python 脚本将此日志数据转换为 JSON
【发布时间】:2022-11-11 22:00:24
【问题描述】:

我有来自 Vivado 模拟器的日志文件,我想将其转换为简单的 JSON 以最终将其可视化。 请建议我使用 python 代码将日志格式化为 JSON。

我试图搜索将日志转换为 JSON,但其中大多数将 .csv(逗号分隔值)转换为 JSON,而我的日志文件包含冒号分隔值。

这是我的日志文件中的一行:

OVL_ERROR:ASSERT_NO_OVERFLOW:达到阈值后计数器未重置:测试表达式将值从允许的最大值 max 更改为 max+1 到 min 范围内的值:严重性 1:时间 430000:counter_tb.no_overflow.ovl_error_t

我希望 JSON 看起来像这样:

{
"Error":"OVL_Error",
"Assertion":"ASSERT_NO_OVERFLOW",
"Message":"Counter_did_not_reset_after_reaching_Threshold",
"Coverage":"Test expression changed value from allowed maximum value max to a value in the range max+1 to min",
"Severity":"1",
"Time":"430000"
}

有没有可能这样做。

谢谢。

【问题讨论】:

  • 基本上 - 在: 处拆分日志行,然后将带有键的相应值压缩到字典中并写为 JSON。显示您的代码并提出具体问题

标签: python json formatting


【解决方案1】:

使用zip() 函数和dict-comprehension 的解决方案。

SeverityTime 被转换为整数。

line = "OVL_ERROR : ASSERT_NO_OVERFLOW : Counter did not reset after reaching Threshold : Test expression changed value from allowed maximum value max to a value in the range max+1 to min : severity 1 : time 430000 : counter_tb.no_overflow.ovl_error_t"

logkeys = ("Error", "Assertion", "Message", "Coverage", "Severity", "Time")
logvalues = [x.strip() for x in line.split(":")[:-1]]
logline = {k:v if i <4 else int(v.rsplit(" ", 1)[-1]) for i, (k, v) in enumerate(zip(logkeys, logvalues))}

print(logline)
{'Error': 'OVL_ERROR',
 'Assertion': 'ASSERT_NO_OVERFLOW',
 'Message': 'Counter did not reset after reaching Threshold',
 'Coverage': 'Test expression changed value from allowed maximum value max to a value in the range max+1 to min',
 'Severity': 1,
 'Time': 430000}

【讨论】:

  • 谢谢伙计,得到了我需要的东西,您还可以分享我可以参考为类似格式的其他自定义日志编写 zip() 函数的内容。
  • zip() 是一个内置函数“并行迭代多个可迭代对象,生成元组,每个对象都有一个项目。”官方文档和示例here
【解决方案2】:

你可以这样做:

fileDesc = open('YourFileName', 'r')
fileData = fileDesc.read()
fileDesc.close()

log = []

for line in fileData.splitlines():
    words = [word.strip() for word in line.split(':')]
    log.append({
        'Error': words[0],
        'Assertion': words[1],
        'Message': words[2],
        'Coverage': words[3],
        'Severity': words[4],
        'Time': words[5]
    })

print(log)

【讨论】:

    【解决方案3】:

    你可以试试:

    import json
    
    log_output = "OVL_ERROR : ASSERT_NO_OVERFLOW : Counter did not reset after reaching Threshold : Test expression changed value from allowed maximum value max to a value in the range max+1 to min : severity 1 : time 430000 : counter_tb.no_overflow.ovl_error_t"
    arr_log = log_output.split(" : ")
    dict_write = {"Error":None ,"Assertion":None ,"Message":None ,"Coverage":None ,"Severity":None ,"Time":None}
    counter = 0
    for index in dict_write:
        dict_write[index] = arr_log[counter]
        counter = counter+1
    
    json_object = json.dumps(dict_write, indent=4)
    
    
    with open("log.json", "w") as outfile:
        outfile.write(json_object)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-06-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多