regular expressions

Tim Chase python.list at tim.thechases.com
Thu Apr 15 20:39:53 CEST 2010


On 04/15/2010 11:05 AM, Terry Reedy wrote:
> On 4/15/2010 2:57 AM, chaaana wrote:
>> hi..
>> im parsing the text file containing the details of the testcases
>> failed.From the file i wanted to obtain only the testcase names and
>> enter them in the excel sheet.
>>
>> the pattern of the text file is:
>>
>> FILE : NW_PTH_TFG6_SCEN_4_2_FIFO.c, LINE : 240 TEST FAIL to get entire:
>> 32768 bytes
>> NW_PTH_TFG6_SCEN_4_2_FIFO.c 340 DATA NOT MATCHING, TEST FAIL
>> TEST FAIL SYSCALL_TEST_SCEN_4_1_ALL.c at line 355
>> FILE:US_TFG7_SCEN_4_1.c,LINE:189, Server side TEST FAIL
>> FAIL BREW_SCEN_4_1.c 219: can't mount
>> FAIL BREW_SCEN_4_1.c 121: can't umount
>> <<   BREW_SCEN_4_1.c TEST FAIL errno:No such file or directory>>
>> <<   BREW_SCEN_4_1.c TEST FAIL>>   error:Invalid argument
>
> I do not see any consistent pattern in the above lines.
> What output do you see from filtering them?

My guess is that it's the "FILE:...LINE:..." line(s) that the OP 
is interested in, in which case one could do something like

   r = re.compile(r'^FILE\s*:\s*(.*?),\s*LINE\s*:\s*(\d+)')
   for line in file('input.txt'):
     m = r.match(line)
     if m:
       print m.group(1), m.group(2)

Alternatively, if you're in the regexp-avoiding camp, you might 
be able to get away with

   LINE_BIT = ', LINE : '
   for line in file('input.txt'):
     if line.startswith('FILE :') and LINE_BIT in line:
       leader, _ = line.split(LINE_BIT, 1)
       _, fname = leader.split(":", 1)
       fname = fname.strip()
       print repr(fname)

but that relies on the LINE_BIT remaining constant (the 
"LINE:189" doesn't have spaces where the first "LINE : 240" does 
have extra spaces; and the same with the extra spaces around the 
"FILE" portion).

-tkc







More information about the Python-list mailing list