You can use a generator of tuples of chunks:
lines = '''soeqashriabsokxz
gajqjrvtxwxigbuy
iyhhzvjdflwybdbx
cycnqfptcuknuwlt'''.split()
def pairs(s, n=2, m=2):
for i in range(0, len(s), n+m):
yield (s[i:i+n], s[i+n:i+n+m])
for l in lines:
# printing here for the example
# but you can save to file
print('\n'.join([''.join(e) for e in zip(*pairs(l))]))
Output:
soasiaok
eqhrbsxz
gajrxwgb
jqvtxiuy
iyzvflbd
hhjdwybx
cyqfcuuw
cnptknlt
generalization
Here is the generalization to split each line in an arbitrary number of lines with an arbitrary number of characters per line:
def pairs(s, k):
x = 0
for i in range(0, len(s), sum(k)):
yield [s[x:(x:=x+e)] for e in k]
def split(lines, k=(2, 2)):
for l in lines:
chunks = zip(*pairs(l, k=k))
print('\n'.join([''.join(e) for e in chunks]))
examples:
split(lines, k=(1,2,3))
sho
oerikx
qasabsz
gvg
ajtxbu
qjrwxiy
ijb
yhdfdb
hzvlwyx
cpu
yctcwl
nqfuknt
split(lines, k=(5,0,1))
soeqahriabokxz
ss
gajqjvtxwxgbuy
ri
iyhhzjdflwbdbx
vy
cycnqptcukuwlt
fn