python日志解析用conu创建重复url和大小dict

2024-05-19 05:52:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我想从下面的格式解析日志文件,有多个URL是重复的,我要计算每个URL的总大小以及计算特定类型URL的大小,最好的方法是什么?在

/images/img81a.jpg 6620
/images/img88a.jpg 6990
/images/img80b.jpg 5909 
/images/swb-30-furniture.gif 6216 
/images/button-arrow.png 498
/images/button-arrow-down.png 484 
/images/img81a.jpg 6620 
/images/img80b.jpg 5909 
/images/back-to-top_off.gif 1506 
/images/new-logo.gif 3377 
/images/img81a.jpg 6620        

Result:

Total size computation: 11503

所有特定类型的URL大小计算:

^{pr2}$

我将所有大小值附加到list并执行加法以获得总大小结果,但是对于类似的url,大小计算需要二维dict。我不打算去创造。在

 a['/images/img81a.jpg'][6620] = 3
 a['/images/img88a.jpg'][6990] = 1
 a['/images/img80b.jpg'][5909] = 2
 like wise ...

Tags: 文件方法url类型png格式buttongif
1条回答
网友
1楼 · 发布于 2024-05-19 05:52:47

假设您在一个列表中列出了所有行:

with open('log.txt') as f:
    dico = dict()
    total_value = 0
    for line in f:
        #Feeding the dictionary
        split_array = line.split()
        possible_key = split_array[0]
        value = int(split_array[1])

        #If url has already been processed, update the value
        #Else, initialize the entry in the dictionary => default=0
        dico[possible_key] = dico.get(possible_key, default=0) + value

        #Updating the global sum
        total_value = total_value + value

用法:

^{pr2}$

相关问题 更多 >

    热门问题