Note that there are some explanatory texts on larger screens.

plurals
  1. POLoop within loop python
    primarykey
    data
    text
    <p>I have a log file with several hundred thousand lines. </p> <p>I am looping through these lines to find any line with some specific text, for example: <code>!!event!!</code>.<br> Then, once an <code>!!event!!</code> line is found, I need to continue looping after this <code>!!event!!</code> line until I find the next 3 lines which contain their own specific text <code>('flag1', 'flag2', and 'flag3')</code>.<br> Once I find the third line <code>('flag3')</code>, I then want to continue looping for the next <code>!!event!!</code> line and repeat the former process until there are no more events. </p> <p>Does anyone have suggestions on ways I structure my code to accomplish this? </p> <p>For example: </p> <pre><code>f = open('samplefile.log','r') for line in f: if '!!event!!' in line: L0 = line #then get the lines after L0 containing: 'flag1', 'flag2', and 'flag3' # below is a sample log file #I am not sure how to accomplish this #(I am thinking a loop within the current loop) #I know the following is incorrect, but the intended result would be able to yield something like this: if "flag1" in line: L1 = line.split() if "flag2" in line: L2 = line.split() if "flag3" in line: L3 = line.split() print 'Event and flag times: ', L0[0], L1[0], L2[0], L3[0] </code></pre> <p>samplefile.log</p> <pre><code>8:41:05 asdfa 32423 8:41:06 dasd 23423 8:41:07 dfsd 342342 8:41:08 !!event!! 23423 8:41:09 asdfs 2342 8:41:10 asdfas flag1 8:41:11 asda 42342 8:41:12 sdfs flag2 8:41:13 sdafsd 2342 8:41:14 asda 3443 8:41:15 sdfs 2323 8:41:16 sdafsd flag3 8:41:17 asda 2342 8:41:18 sdfs 3443 8:41:19 sdafsd 2342 8:41:20 asda 3443 8:41:21 sdfs 4544 8:41:22 !!event!! 5645 8:41:23 sdfs flag1 8:41:24 sadfs flag2 8:41:25 dsadf 32423 8:41:26 sdfa 23423 8:41:27 sdfsa flag3 8:41:28 sadfa 23423 8:41:29 sdfas 2342 8:41:30 dfsdf 2342 </code></pre> <p>the code from this sample should print:</p> <pre><code>Event and flag times: 8:41:08 8:41:10 8:41:12 8:41:16 Event and flag times: 8:41:22 8:41:23 8:41:24 8:41:27 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload