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

2024-10-03 09:08:41 发布

您现在位置: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']

你能帮我弄清楚怎么完成这个吗


Tags: 文件ofnumberasynctimednsrequestline