在Python中解析文件/列

2024-10-03 19:29:04 发布

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

10:01:36 adcli
10:01:36 sshd[
10:01:37 adcli
10:01:37 adcli
10:01:37 sshd[
10:01:37 adcli
10:01:37 adcli
10:01:37 sshd[
10:01:37 sshd[
10:01:37 adcli
10:01:37 adcli
10:01:37 sshd[
10:01:37 sshd[
10:01:37 adcli
10:01:37 adcli
10:01:37 adcli
10:01:37 runma
10:01:37 runma
10:01:37 sshd[
10:01:37 adcli
10:01:37 adcli
10:01:37 sshd[
10:01:37 sshd[
10:01:37 adcli
10:01:37 adcli
10:01:37 sshd[
10:01:37 roots
10:01:37 sshd[
10:01:37 adcli
10:01:37 adcli
10:01:37 adcli
10:01:37 sshd[
10:01:37 adcli
10:01:37 adcli
10:01:37 sshd[
10:01:37 sshd[

这是我的方法:(我知道这不是一个完整的,但)

    import re
    i="sshd"
    j="apached"
    k="wexd"
    count_a=0;
    count_b=0;
    count_c=0;
    file=open("hex01.txt","r")
    for line in file:
       for datestamp in line[0:5]
            if line.match("datestamp"):
                   print    datestamp,m=line.count("sshd"),n=line.count("apached"),0=line.count     ("wexd"),t=m+n+0  

这是我试图用Python处理的示例输入数据。我知道使用bash获得输出相当容易,但是我正在学习Python,我觉得获得所需的输出相当困难。任何帮助都将被感激,我甚至不需要一个完美的代码,但算法和适当的python库就足够了。输出应该是

A进程计数,B进程计数,总计

例如:10:01:37 10,2,1,13—表示10 sshd、2adcli和1来自上述日志文件


Tags: 方法infor进程countlinefile计数
2条回答
1.create_unique_dict_time /*have no clue how to make this?
2. create_unique_dict_process /*
 for line in file.open("a.txt","r"):
     `time,process = line.split()
     create_unique_dic_time 
     create_unique_dict_process     
 for line in file.open("a.txt","r"):
    time,process=line.split()
      while time= {loop through each of the elements in 1.}
              if process={loop through each elements. in 2.}
               print time,[{element1,...}],sum{elements}

@Padriac,sum是所有进程的总和。在上一个示例中,如果您看到输出:10:01:37 10,2,1,13 13引用所有进程的总和10+2+1,那么该总和仍然缺失。祝你新年快乐!你知道吗

当我们看到一个新的时间戳时,可以使用dict.setdefault添加一个新的dict,并增加sshd等。。每次我们看到键时,键值为1:

d = {}
with open("in.txt") as f:
    for line in f: 
        # split into time and sshd etc..
        time, res = line.split()
        # use setdefault so we get a new dict for each timestamp and set each new key's value to 0
        d.setdefault(time, {}).setdefault(res[:-1],0)
        d[time][res[:-1]] += 1

from pprint import pprint as pp
pp(d)
{'10:01:36': {'adcl': 1, 'sshd': 1},
'10:01:37': {'adcl': 20, 'root': 1, 'runm': 2, 'sshd': 10}}

您还可以使用collections.Counterdict:

from collections import Counter
d = {}
with open("in.txt") as f:
    for line in f:
        time, res = line.split()
        d.setdefault(time, Counter())
        d[time].update([res[:-1]])

相关问题 更多 >