nested grouping in Python

HI All

Is there a way of nested grouping in python
I wish to format a file as given below
Input File:

Item: 1
sub1
A,10,x,5
B,345,v,7
C,67,u,9
sub2
A,09,9,.09
B,65,3..34
C,8,8,1

Item: 2
sub1
A,10,x,5
B,345,v,7
C,67,u,9
sub2
A,09,9,.23
B,65,3..2
C,8,8,.5

Item: 3
sub1
A,10,x,5
B,345,v,7
C,67,u,9
sub2
A,09,9,.5
B,65,3..4
C,8,8,6

group by Item and then group by sub so that I retain the sub1 and append the last element in sub2 to the output

Required output:
Item: 1
A,10,x,5,.09
B,345,v,7,.34
C,67,u,9,1
Item: 2
A,10,x,5,.23
B,345,v,7,2
C,67,u,9,.5
Item: 3
A,10,x,5,.5
B,345,v,7,.4
C,67,u,9,6

Any suggestion will be immensely appreciated
Thanks
Abid

Your output example contradicts your text, no trace of sub2.

Thanks for looking into it .

I meant the last element of sub2 which is in this case .09 gets appended to whole of sub1

Item: 1
Example
sub1
A,10,x,5
sub2
A,09,9,.09

.09 of sub2 is appended to sub1

output:
Item: 1
A,10,x,5,.09

Well, that is a bit messy: parse and hold. You need to hold sub1 in variables until you get to sub2 to collect the additions to A and whatever that will go on the new, merged sub's output.

do you know any module which I can use here in python

Well, you will have to write to your i/o files' schema. You might leverage any CSV modules Python might have, or other delimited text parsers.

Thanks but I was hoping for some grouping modules it might have was trying with itertools but not sure if that is the way to do it ...