Python: extract pattern from stdout and save in csv
<p>I have log files of around 20000-30000 lines long, they contain all sort of data , each line starting with current time stamp, followed by path of files/linu numbers and then value of objects added with some additional (unnecessary info).</p>

<pre><code>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
</code></pre>

<p>....
   ...
   ...
   Same info... for new object 
There are 30-40 object groups per file and they varies (between ID 0-3) </p>

<pre><code>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
</code></pre>

<p>...
   ...
   ...</p>

<p>(here there isn't any object with Id 0)
    1           1           50
    2           51          109
     ...</p>

<pre><code>Or may be store in csv file like
   0,1,55
   1,56,67
   2,68,110
</code></pre>

<pre><code>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))
</code></pre>

<p><hr>
2.6 version:</p>

<pre><code>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))
</code></pre>

<p><strong>out.csv</strong> (<code>in.txt</code> as in OP)</p>

<pre><code>0,1,55
1,56,67
2,68,110
</code></pre>

python csv pattern-matching export-to-csv