82373


下記iperfというツールのログですが、出力結果の一番左に日時を30回づつ追記する  
処理を行いたいと思っております。  
どの様に、テキストの一番左に追記するのかが分からず悩んでおります。  
ご教授いただければと思います。

[code]

    import re
    
    with open('iperf-result.log', 'r', encoding='shift-jis') as f:
        contents = f.readlines()
    
    #出力したログから、日付部分だけを抽出しリストに保管しておく
    date = []
    for i, content in enumerate(contents):
        if re.match(r'2021/*.', content):
            print(i, content.strip('\n'))
            date.append(content.strip('\n'))
    
    # 30行で一回分の計測結果がまとめられているため、保存した日付を30回取り出せるか
    #試したコード
    # for d in date:
    #    for i in range(30):
    #        print(i, d)
    
    # テキストファイルを追記モードで開く
    with open('perf-result.log', 'a', encoding='shift-jis') as f:
        write = f.write()
    # ここから、どうしたら良いのか。
    
[/code]

実現したい再集計↓

[code]

    2021/09/0217:15:11.24 2021/09/0217:15:11.24 
    2021/09/0217:15:11.24  Connecting to host 192.168.1.1, port 5201
    2021/09/0217:15:11.24  [  4] local 192.168.1.1 port 62946 connected to 192.168.1.2 port 5201
    2021/09/0217:15:11.24  [ ID] Interval           Transfer     Bandwidth
    2021/09/0217:15:11.24  [  4]   0.00-1.00   sec  5.00 MBytes  41.9 Mbits/sec                  
    2021/09/0217:15:11.24  [  4]   1.00-2.01   sec  3.25 MBytes  27.0 Mbits/sec                  
    2021/09/0217:15:11.24  [  4]   2.01-3.00   sec  15.4 MBytes   130 Mbits/sec  
    
[/code]

生ログ（iperf-result.log）↓

[code]

    2021/09/0217:15:11.24 
    Connecting to host 192.168.1.1, port 5201
    [  4] local 192.168.1.1 port 62946 connected to 192.168.1.2 port 5201
    [ ID] Interval           Transfer     Bandwidth
    [  4]   0.00-1.00   sec  5.00 MBytes  41.9 Mbits/sec                  
    [  4]   1.00-2.01   sec  3.25 MBytes  27.0 Mbits/sec                  
    [  4]   2.01-3.00   sec  15.4 MBytes   130 Mbits/sec                  
    [  4]   3.00-4.01   sec  13.4 MBytes   111 Mbits/sec                  
    [  4]   4.01-5.02   sec  4.00 MBytes  33.4 Mbits/sec                  
    [  4]   5.02-6.02   sec  5.75 MBytes  48.3 Mbits/sec                  
    [  4]   6.02-7.00   sec  8.88 MBytes  75.7 Mbits/sec                  
    [  4]   7.00-8.00   sec  9.62 MBytes  80.5 Mbits/sec                  
    [  4]   8.00-9.01   sec  20.1 MBytes   168 Mbits/sec                  
    [  4]   9.01-10.00  sec  11.8 MBytes  98.8 Mbits/sec                  
    [  4]  10.00-11.01  sec  27.0 MBytes   226 Mbits/sec                  
    [  4]  11.01-12.00  sec  26.2 MBytes   221 Mbits/sec                  
    [  4]  12.00-13.00  sec  28.1 MBytes   236 Mbits/sec                  
    [  4]  13.00-14.00  sec  29.1 MBytes   244 Mbits/sec                  
    [  4]  14.00-15.00  sec  31.2 MBytes   262 Mbits/sec                  
    [  4]  15.00-16.00  sec  30.1 MBytes   253 Mbits/sec                  
    [  4]  16.00-17.00  sec  23.8 MBytes   199 Mbits/sec                  
    [  4]  17.00-18.01  sec  33.0 MBytes   276 Mbits/sec                  
    [  4]  18.01-19.00  sec  31.9 MBytes   268 Mbits/sec                  
    [  4]  19.00-20.00  sec  28.1 MBytes   236 Mbits/sec                  
    - - - - - - - - - - - - - - - - - - - - - - - - -
    [ ID] Interval           Transfer     Bandwidth
    [  4]   0.00-20.00  sec   386 MBytes   162 Mbits/sec                  sender
    [  4]   0.00-20.00  sec   386 MBytes   162 Mbits/sec                  receiver
    
    iperf Done.
    2021/09/0217:25:00.22 
    Connecting to host 192.168.1.1, port 5201
    [  4] local 192.168.1.1 port 62946 connected to 192.168.1.2 port 5201
    [ ID] Interval           Transfer     Bandwidth
    [  4]   0.00-1.00   sec  29.1 MBytes   244 Mbits/sec                  
    [  4]   1.00-2.00   sec  30.6 MBytes   257 Mbits/sec                  
    [  4]   2.00-3.00   sec  32.1 MBytes   270 Mbits/sec                  
    [  4]   3.00-4.00   sec  30.9 MBytes   259 Mbits/sec                  
    [  4]   4.00-5.00   sec  32.6 MBytes   274 Mbits/sec                  
    [  4]   5.00-6.00   sec  28.5 MBytes   238 Mbits/sec                  
    [  4]   6.00-7.00   sec  28.4 MBytes   238 Mbits/sec                  
    [  4]   7.00-8.00   sec  28.5 MBytes   239 Mbits/sec                  
    [  4]   8.00-9.00   sec  31.2 MBytes   262 Mbits/sec                  
    [  4]   9.00-10.00  sec  33.6 MBytes   283 Mbits/sec                  
    [  4]  10.00-11.00  sec  31.4 MBytes   262 Mbits/sec                  
    [  4]  11.00-12.00  sec  32.2 MBytes   272 Mbits/sec                  
    [  4]  12.00-13.00  sec  31.8 MBytes   266 Mbits/sec                  
    [  4]  13.00-14.00  sec  31.5 MBytes   265 Mbits/sec                  
    [  4]  14.00-15.00  sec  33.2 MBytes   278 Mbits/sec                  
    [  4]  15.00-16.00  sec  32.5 MBytes   272 Mbits/sec                  
    [  4]  16.00-17.00  sec  29.8 MBytes   250 Mbits/sec                  
    [  4]  17.00-18.00  sec  27.9 MBytes   233 Mbits/sec                  
    [  4]  18.00-19.00  sec  25.6 MBytes   215 Mbits/sec                  
    [  4]  19.00-20.00  sec  23.8 MBytes   199 Mbits/sec                  
    - - - - - - - - - - - - - - - - - - - - - - - - -
    [ ID] Interval           Transfer     Bandwidth
    [  4]   0.00-20.00  sec   605 MBytes   254 Mbits/sec                  sender
    [  4]   0.00-20.00  sec   605 MBytes   254 Mbits/sec                  receiver
    
    iperf Done.
    
[/code]


CC BY-SA 4.0
2021-09-09T06:44:47.540
82370
2021-09-09T09:37:26.487
2021-09-09T07:47:02.710
3060
question
python python3
テキストの一番左に追記する方法（python）


行を追いかけながら日付を見つけて記録・更新していくのはどうでしょうか。

[code]

    DATE_PATTERN = r'\d{4}/[01]\d/[0-3]\d'
    TIME_PATTERN = r'[0-2]\d:[0-5]\d:[0-6]\d\.\d{2}'
    
    
    def timestamped_lines(lines):
        timestamp = '????/??/????:??:??.??'
        TIMESTAMP_REGEXP = re.compile(DATE_PATTERN + TIME_PATTERN + r'\s*$')
    
        for line in lines:
            m = TIMESTAMP_REGEXP.match(line)
            if m:
                timestamp = line.strip()
                sep = ' '
            else:
                sep = '  '
            yield f"{timestamp}{sep}{line}"
    
    
    with open('perf-result.log', 'a', encoding='shift-jis') as f:
        for line in timestamped_lines(contents):
            f.write(line)
    
[/code]


CC BY-SA 4.0
2021-09-09T07:44:03.843
82373
2021-09-09T07:50:17.040
2021-09-09T07:50:17.040
9464
9464
82370
answer


読み込むファイルが 'iperf-result.log' で追記するファイルが 'perf-result.log' であり別のファイルということですね。  
素直に書くのであれば、こんな感じと思います。

[code]

    with open('perf-result.log', 'a', encoding='shift-jis') as f:
        for content in contents:
            if content.strip('\n') in date:
                timestamp = date[date.index(content.strip('\n'))]
            f.write(timestamp + ' ' + content)
    
[/code]

必ず「30行で一回分の計測結果がまとめられている」のであれば次のようにもできます。

[code]

    new_contents = [date[i//30] + ' ' + content for i, content in enumerate(contents)]
    with open('perf-result.log', 'a', encoding='shift-jis') as f:
        f.writelines(new_contents)
    
[/code]


CC BY-SA 4.0
2021-09-09T08:33:47.197
82376
2021-09-09T08:33:47.197
41756
82370
answer


以下は `re.sub()` を使う方法です。

[code]

    import re
    
    with open('iperf-result.log', 'r') as r, open('perf-result.log', 'w') as w:
      log = re.sub(
        '\n(.+?)\nConnecting to host(.|\n)+?\niperf Done.',
        lambda m: re.sub(r'\n', f'\n{m.group(1)}', m.group(0)),
        '\n' + r.read())
      w.write(log[1:])
    
[/code]

※ ファイルへの保存は append(`mode='a'`)ではなく write にしています。

**perf-result.log**

[code]

    2021/09/0217:15:11.24 2021/09/0217:15:11.24 
    2021/09/0217:15:11.24 Connecting to host 192.168.1.1, port 5201
    2021/09/0217:15:11.24 [  4] local 192.168.1.1 port 62946 connected to 192.168.1.2 port 5201
    2021/09/0217:15:11.24 [ ID] Interval           Transfer     Bandwidth
                         :
    
    2021/09/0217:15:11.24 
    2021/09/0217:15:11.24 iperf Done.
    2021/09/0217:25:00.22 2021/09/0217:25:00.22 
    2021/09/0217:25:00.22 Connecting to host 192.168.1.1, port 5201
    2021/09/0217:25:00.22 [  4] local 192.168.1.1 port 62946 connected to 192.168.1.2 port 5201
    2021/09/0217:25:00.22 [ ID] Interval           Transfer     Bandwidth
                         :
    
    2021/09/0217:25:00.22 [  4]   0.00-20.00  sec   605 MBytes   254 Mbits/sec                  receiver
    2021/09/0217:25:00.22 
    2021/09/0217:25:00.22 iperf Done.
    
[/code]


CC BY-SA 4.0
2021-09-09T09:37:26.487
82380
2021-09-09T09:37:26.487
47127
82370
answer