需要帮助将日志文件转换为json格式

时间:2020-10-20 10:55:24

标签: python

我是Python编程的新手。我需要有关如何将下面的日志文件转换为JSON文件的帮助。

我的日志文件:

DNSCacheInfo

enabled             yes
CacheEntries        0/1024
HitRatio            0/0 (  0.00%)


Async DNS disabled


Performance Counters

Total number of requests                  55
Average Request processing time       0.0006
Total Request processing time         0.0310


default-bucket (Default bucket)

Counter Name               Average         Total      Percent
-------------------------------------------------------------
Number of Requests                            55    (100.00%)
Number of Invocations                        444    (100.00%)
Latency                     0.0001        0.0050    ( 16.13%)
Function Processing Time    0.0005        0.0260    ( 83.87%)
Total Response Time         0.0006        0.0310    (100.00%)

我需要像这样的输出

{ DNSCacheInfo : { enabled : yes , CacheEntries : 0/1024 , HitRatio : 0/0 (  0.00%) }, Async DNS : disabled ,  Performance Counters : {  Total number of requests : 55 ,  Average Request processing time : 0.0006 ,  Total Request processing time : 0.0310 } , default-bucket : { [ Counter Name : Number of Requests , Average : "" , Total : 55 , Percent : (100.00%)],[ Counter Name : Number of Invocations , Average : "" , Total :  444 , Percent : (100.00%)] , ..}}                         

尝试以下代码:

datacomplete = {}
with open("test.log") as f:
    lines = f.readlines()
    
    for line in lines:
            if 'Async DNS' in line:
                val1 = line.split('Async DNS')[1].split(':')[1].strip()
                print(val1)
                datacomplete["Async DNS"]=val1
            if 'DNSCacheInfo' in line:
                DNSCacheInfo={}
                DNSCacheInfo_headings=['enabled','CacheEntries','HitRatio']

您能帮我弄清楚如何完成此操作吗?

0 个答案:

没有答案