Reading data file with varying number of columns python -
i have data-file first 8 lines of this. (after substituting actual values letters clarity of question)
a,b,c d e,f g,h i,j,k l m,n o,p
these represent data transformers in electric network. first 4 lines information transformer 1, next 4 transformer 2 , on.
the variables a-p can either integers, floating-point numbers or strings
i need write script in python that instead of data 1 transformer being spread onto 4 lines, should on 1 line.
more precisely, above 2 lines converted
a,b,c,d,e,f,g,h i,j,k,l,m,n,o,p
and write data-file.
how do this?
from itertools import izip_longest def grouper(iterable, n, fillvalue=none): "collect data fixed-length chunks or blocks" # grouper('abcdefg', 3, 'x') --> abc def gxx args = [iter(iterable)] * n return izip_longest(fillvalue=fillvalue, *args) open('z.t') f: d = grouper(f, 4) x in d: print ','.join(y.rstrip() y in x) a,b,c,d,e,f,g,h i,j,k,l,m,n,o,p
Comments
Post a Comment