jmunsch jmunsch - 1 month ago 11
Python Question

zip iterator missing last elements how to join lines together

How can I join all lines together without missing the last elements.

zip()
seems to be missing the last elements in this stream.

For example:
seq 0 70 | xargs -L 50 | sed 's/ /,/g'


Input:

seq 0 70 | python -c $'import sys\nfor x in zip(*[iter(sys.stdin)]*50): print(",".join(x).replace("\\n",""))\n'


Wrong Output:

0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49


Expected 50 items per line and last zip contains the rest:

0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49
50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69


Related:


Answer
 $'import sys,itertools\nfor x in itertools.izip_longest(*[iter(sys.stdin)]*50): print(",".join(x).replace("\\n",""))\n'

maybe? this uses izip_longest to not drop extra items at the end