在块中处理文本文件,其中每个块由时间戳分隔

2024-10-02 12:28:41 发布

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

我尝试使用Python解析iostat-xt输出。iostat的奇怪之处在于,每秒钟的输出都要经过多行。例如:

06/30/2015 03:09:17 PM 
avg-cpu:  %user   %nice %system %iowait  %steal   %idle 
           0.03    0.00    0.03    0.00    0.00   99.94 

Device:         rrqm/s   wrqm/s     r/s     w/s    rkB/s    wkB/s avgrq-sz avgqu-sz   await r_await w_await  svctm  %util 
xvdap1            0.00     0.04    0.02    0.07     0.30     3.28    81.37     0.00   29.83    2.74   38.30   0.47   0.00 
xvdb              0.00     0.00    0.00    0.00     0.00     0.00    11.62     0.00    0.23    0.19    2.13   0.16   0.00 
xvdf              0.00     0.00    0.00    0.00     0.00     0.00    10.29     0.00    0.41    0.41    0.73   0.38   0.00 
xvdg              0.00     0.00    0.00    0.00     0.00     0.00     9.12     0.00    0.36    0.35    1.20   0.34   0.00 
xvdh              0.00     0.00    0.00    0.00     0.00     0.00    33.35     0.00    1.39    0.41    8.91   0.39   0.00 
dm-0              0.00     0.00    0.00    0.00     0.00     0.00    11.66     0.00    0.46    0.46    0.00   0.37   0.00 

06/30/2015 03:09:18 PM 
avg-cpu:  %user   %nice %system %iowait  %steal   %idle 
           0.00    0.00    0.50    0.00    0.00   99.50 

Device:         rrqm/s   wrqm/s     r/s     w/s    rkB/s    wkB/s avgrq-sz avgqu-sz   await r_await w_await  svctm  %util 
xvdap1            0.00     0.00    0.00    0.00     0.00     0.00     0.00     0.00    0.00    0.00    0.00   0.00   0.00 
xvdb              0.00     0.00    0.00    0.00     0.00     0.00     0.00     0.00    0.00    0.00    0.00   0.00   0.00 
xvdf              0.00     0.00    0.00    0.00     0.00     0.00     0.00     0.00    0.00    0.00    0.00   0.00   0.00 
xvdg              0.00     0.00    0.00    0.00     0.00     0.00     0.00     0.00    0.00    0.00    0.00   0.00   0.00 
xvdh              0.00     0.00    0.00    0.00     0.00     0.00     0.00     0.00    0.00    0.00    0.00   0.00   0.00 
dm-0              0.00     0.00    0.00    0.00     0.00     0.00     0.00     0.00    0.00    0.00    0.00   0.00   0.00 

06/30/2015 03:09:19 PM 
avg-cpu:  %user   %nice %system %iowait  %steal   %idle 
           0.00    0.00    0.50    0.00    0.00   99.50 

Device:         rrqm/s   wrqm/s     r/s     w/s    rkB/s    wkB/s avgrq-sz avgqu-sz   await r_await w_await  svctm  %util 
xvdap1            0.00     0.00    0.00    0.00     0.00     0.00     0.00     0.00    0.00    0.00    0.00   0.00   0.00 
xvdb              0.00     0.00    0.00    0.00     0.00     0.00     0.00     0.00    0.00    0.00    0.00   0.00   0.00 
xvdf              0.00     0.00    0.00    0.00     0.00     0.00     0.00     0.00    0.00    0.00    0.00   0.00   0.00 
xvdg              0.00     0.00    0.00    0.00     0.00     0.00     0.00     0.00    0.00    0.00    0.00   0.00   0.00 
xvdh              0.00     0.00    0.00    0.00     0.00     0.00     0.00     0.00    0.00    0.00    0.00   0.00   0.00 
dm-0              0.00     0.00    0.00    0.00     0.00     0.00     0.00     0.00    0.00    0.00    0.00   0.00   0.00 

实际上,我需要在“chunks”中解析输出,其中每个chunk由一个时间戳分隔。在

我在看itertools.groupby(),但这似乎并不能完全满足我在这里想要的效果——它似乎更适合于分组行,其中每个行都由一个公共键合并,或者可以使用函数来检查的内容。在

另一个想法是:

^{pr2}$

但这似乎不太管用。在

有没有一种python方法来解析上面的iostat输出,并将其按时间戳分割成块?在


Tags: devicecpuawaitsystemavgniceidleuser
1条回答
网友
1楼 · 发布于 2024-10-02 12:28:41

您可以使用regex:

import re
date_reg = "([0-9]{2}\/[0-9]{2}\/[0-9]{4} [0-9]{2}\:[0-9]{2}\:[0-9]{2} (?:AM|PM))"
def split_by_date(text_iter):
    date = None
    lines = []
    for line in text_iter:
        if re.match(date_reg, line):
            if lines or date:
                yield (date, lines)
            date = datetime.strptime(line.strip(), '%m/%d/%y %H:%M:%S')
            lines = []
        else:
            lines.append(line)

    yield (date, lines)

for date, lines in split_by_date(f):
    # here you have lines for each encountered date
    for line in lines:
        print line

相关问题 更多 >

    热门问题