Python Changing the format of a text file to a new format
Question: I am giving a text file with the format below:

    
    
    3     Bham    Hoover - Vestiva 
    123 234 1 456 876 1 876 745 1
    0
    4     Bham    Vestiva - Greensprings
    235 876 1 647 987 1 098 765 1 234 546 1
    0
    

This goes on for several more lines, but I am trying to convert this format to
the following:

    
    
    Event
    Disconnect branch from bus 123 to 234 circuit 1
    Disconnect branch from bus 456 to 876 circuit 1
    Disconnect branch from bus 876 to 745 circuit 1
    end
    Event 
    Disconnect branch from bus 235 to 876 circuit 1
    Disconnect branch from bus 647 to 987 circuit 1
    Disconnect branch from bus 098 to 765 circuit 1
    Disconnect branch from bus 234 to 546 circuit 1
    end
    


Answer: 
    from itertools import islice
    with open('file.txt', 'r') as f:
        # iterate over every 3rd line, starting with the 2nd
        for line in islice(f, 1, None, 3):
            parts = line.split()
            print 'Event'
            # iterate over 3-element chunks
            for x in zip(*(iter(parts),) * 3):
                print 'Disconnect branch from bus %s to %s circuit %s' % x
            print 'end'
    

Output:

    
    
    Event
    Disconnect branch from bus 123 to 234 circuit 1
    Disconnect branch from bus 456 to 876 circuit 1
    Disconnect branch from bus 876 to 745 circuit 1
    end
    Event
    Disconnect branch from bus 235 to 876 circuit 1
    Disconnect branch from bus 647 to 987 circuit 1
    Disconnect branch from bus 098 to 765 circuit 1
    Disconnect branch from bus 234 to 546 circuit 1
    end
    

Credits to the chunking code go to [Iteration over list
slices](http://stackoverflow.com/questions/1335392/iteration-over-list-
slices/1335618#1335618)

