Python:从stdout中提取模式并保存在cs中

2024-09-28 05:18:25 发布

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

我有大约20000-30000行长的日志文件,它们包含所有类型的数据,每行以当前时间戳开始,然后是文件路径/linu编号,然后是添加了一些附加(不必要的信息)的对象值。你知道吗

2016/08/31 17:27:43/usr/log/data/old/objec: 540: Adjustment Stat
2016/08/31 17:27:43/usr/log/data/old/objec: 570: Position: 1
2016/08/31 17:27:43/usr/log/data/old/object::1150: Adding new object in    department xxxx
2016/08/31 17:27:43/usr/log/data/old/file1.java:: 728: object ID: 0
2016/08/31 17:27:43/usr/log/data/old/file2.java:: 729: Start location:1
2016/08/31 17:27:43/usr/log/data/old/file1.java:: 730: End location:55
2016/08/31 17:27:43/usr/log/data/old/: 728: object ID: 1
2016/08/31 17:27:43/usr/log/data/old/: 729: Start location:56
2016/08/31 17:27:43/usr/log/data/old/: 730: End location:67
2016/08/31 17:27:43/usr/log/data/old/: 728: object ID: 2
2016/08/31 17:27:43/usr/log/data/old/: 729: Start location:68
2016/08/31 17:27:43/usr/log/data/old/: 730: End location:110
Timer to Calculate location of object x took 0.004935 seconds

。。。。 ... ... 相同的信息。。。对于新对象 每个文件有30-40个对象组,它们各不相同(在ID 0-3之间)

I want to extract information (next line after Adjustment Stat)and save in a text file like
Position ObjectID   StartLocation   EndLocation
           0             1              55
           1             56             67
           2             68             110

。。。 ... ... 你知道吗

(此处没有任何Id为0的对象) 1 1 50 2 51 109 ... 你知道吗

Or may be store in csv file like
   0,1,55
   1,56,67
   2,68,110

Tags: 文件对象inlog信息iddataobject
1条回答
网友
1楼 · 发布于 2024-09-28 05:18:25
import csv

with open('out.csv', 'w') as output_file, open('in.txt') as input_file:
    writer = csv.writer(output_file)
    for l in input_file:
        if 'object ID:' in l:
            object_id = l.split(':')[-1].strip()
        elif 'Start location:' in l:
            start_loc = l.split(':')[-1].strip()
        elif 'End location:' in l:
            end_loc = l.split(':')[-1].strip()
            writer.writerow((object_id, start_loc, end_loc))


2.6版本:
import csv
import contextlib

with contextlib.nested(open('out.csv', 'w'), open('in.txt')) as (output_file, input_file):
    writer = csv.writer(output_file)
    for l in input_file:
        if 'object ID:' in l:
            object_id = l.split(':')[-1].strip()
        elif 'Start location:' in l:
            start_loc = l.split(':')[-1].strip()
        elif 'End location:' in l:
            end_loc = l.split(':')[-1].strip()
            writer.writerow((object_id, start_loc, end_loc))

输出.csvin.txt如在OP中)

0,1,55
1,56,67
2,68,110

相关问题 更多 >

    热门问题