Q:

Reading data file with varying number of columns python

I have a data-file the first 8 lines of which look like this. (after substituting actual
values by letters for clarity of this question)
    a,b,c
    d
    e,f
    g,h
    i,j,k
    l
    m,n
    o,p

These represent data about transformers in an electric network. The first 4
lines are information about transformer 1, the next four about transformer 2 
and so on.
The variables a-p can are either integers, floating-point numbers or strings
I need to write a script in python so that that instead of data for one transformer being spread onto 4 lines, it should all be on one line. 
More precisely, I would like the above 2 lines  to be converted into 
  a,b,c,d,e,f,g,h
  i,j,k,l,m,n,o,p

and write this to another data-file.
How do I do this?

A:

Use the grouper recipe from itertools
from itertools import izip_longest
def grouper(iterable, n, fillvalue=None):
    "Collect data into fixed-length chunks or blocks"
    # grouper('ABCDEFG', 3, 'x') --> ABC DEF Gxx
    args = [iter(iterable)] * n
    return izip_longest(fillvalue=fillvalue, *args)

with open('z.t') as f:
    d = grouper(f, 4)
    for x in d:
            print ','.join(y.rstrip() for y in x) 

a,b,c,d,e,f,g,h
i,j,k,l,m,n,o,p

