Note that there are some explanatory texts on larger screens.

plurals
  1. POReturn All Matching Lines in a Logfile
    primarykey
    data
    text
    <p>I need some help with looping, or a better way to go about this. The answer may be obvious, but I'm new here and feel a mental block right now: I have a log file that looks like this and I am trying to match all lines with the same ID: so I can later compare the values of matched ID's. I am able to match the first lines, but then my loop seems to terminate. I am not sure what I'm doing wrong, or if there is a better approach altogether. Any help is much appreciated!</p> <p>Some notes:</p> <ul> <li>when I split the lines, the XYZ ID column is indexed at line[2], where len(line) == 11.</li> <li>I am trying to loop through the file and for each line, create an inner loop which scans the remaining lines of the file to find a 'match'.</li> <li>If a match is found, I want to return this so I can compare values</li> <li>The trouble is my code seems to break after the first match is found, thus returning only the first match found</li> </ul> <p>below is my code and a sample of the log file that I'm working with (includes some edited strings just to keep some business data private). The actual logfile includes commas, which were removed before I pasted into this forum:</p> <pre><code>f = open('t.log','r') for line in f: aline = line.replace(',','').split() if len(aline)==11: for line in f: bline = line.replace(',','').split() if len(bline)==11 and aline[2]==bline[2]: print 'a: ', aline print 'b: ', bline #t.log [13:40:19.xxx009] status ------- [13:40:19.xxx013] status XYZ -4 -5675.36 quote 449.70/- 449.78 avg 1418.84 -7474.48 0.134 -55.630 -395.148 [13:40:19.xxx021] status XYZ ID:22P00935xxx -4 3.92 quote: 0.98/ 1.02 avg: -0.98 -0.16 [13:40:19.xxx024] status XYZ ID:22C0099xxx0 -2 26.4 quote: 11.60/ 11.85 avg: -13.20 2.70 [13:40:19.xxx027] status XYZ ID:22P0099xxx0 10 -17.18 quote: 1.86/ 1.90 avg: -1.72 1.42 [13:40:19.xxx029] status XYZ ID:22C00995xxx 4 -42.5 quote: 8.20/ 8.30 avg: -10.62 -9.70 [13:40:19.xxx031] status XYZ ID:22P00995xxx 2 9.66 quote: 3.30/ 3.40 avg: 4.83 16.26 [13:40:19.xxx535] status total xx5.52 [13:41:20.xxx688] status ------- [13:41:20.xxx691] status XYZ -4 -5675.36 quote 449.83/- 449.99 avg 1418.84 -7475.32 -0.374 -213.006 -39.391 [13:41:20.xxx701] status XYZ ID:22P00935xxx -4 3.92 quote: 0.96/ 1.00 avg: -0.98 -0.08 [13:41:20.xxx704] status XYZ ID:22C0099xxx0 -2 26.4 quote: 11.65/ 11.90 avg: -13.20 2.60 [13:41:20.xxx708] status XYZ ID:22P0099xxx0 10 -17.18 quote: 1.83/ 1.87 avg: -1.72 1.12 [13:41:20.xxx712] status XYZ ID:22C00995xxx 4 -42.5 quote: 8.20/ 8.30 avg: -10.62 -9.70 [13:41:20.xxx716] status XYZ ID:22P00995xxx 2 9.66 quote: 3.30/ 3.35 avg: 4.83 16.26 [13:41:20.xxx718] status XYZ ID:22C0095xxx0 -10 35.6 quote: 5.40/ 5.50 avg: -3.56 -19.40 [13:41:20.001362] status total xx6.68 Result: $ python pnlcomp.py a: ['[13:40:19.000021]', 'statusAAPL', '130322P00435000', '-4', '3.92', 'quote:', '0.98/', '1.02', 'avg:', '-0.98', '-0.16'] b: ['[13:41:20.000701]', 'statusAAPL', '130322P00435000', '-4', '3.92', 'quote:', '0.96/', '1.00', 'avg:', '-0.98', '-0.08'] </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