Python - parsing nested information and provide it in proper format from log file

Peter Otten __peter__ at web.de
Fri Feb 20 08:10:30 EST 2015


Jay T wrote:

>  have some log file which has nested data which i want to filter and
>  provide specific for student with total counts
> 
> Here is my log file sample:
> Student name is ABC
> Student age is 12
> student was late
> student was late
> student was late
> Student name is DEF
> student age is 13
> student was late
> student was late
> 
> i want to parse and show data as Student name, student age , number of
> counts how many times student was late e:g Name Age TotalCount
> ABC 12   3
> DEF 13    2
> 
> Please help me with solution that will be really grateful.

What have you tried? Please show us some code.

The basic idea would be to iterate over the lines and split the current line 
into words. 

If the second word is "name" and it's not the first iteration print the 
student's name, age, and was_late count. Then set the name variable to the 
new name and reset age and was_late to 0. To detect the first iteration you 
can set 

name = None

before you enter the loop and then check for that value before printing:

if name is not None:
    ... # print student data

If the second word is "age" convert the 4th word to integer and set the age 
variable.

If the second word is "was" increment the was_late counter.

Remember that when the loop ends and the file was not empty you have one 
more student's data to print.






More information about the Python-list mailing list