Home > Net >  python move every second pair of letters to a new line in a multi line document
python move every second pair of letters to a new line in a multi line document

Time:03-31

So I have a multi line text file,

soeqashriabsokxz
gajqjrvtxwxigbuy
iyhhzvjdflwybdbx
cycnqfptcuknuwlt

and I would like to take every second pair of letters and make a new line under each line from those letters

soasiaok
eqhrbsxz
gajrxwgb 
jqvtxiuy
iyzvflbd 
hhjdwybx
cyqfcuuw 
cnptknlt

does anybody ahve any ideas?

CodePudding user response:

I would do the following:

from itertools import chain

with open('in.txt') as input_file, open('out.txt', 'w') as output_file:
  for line in input_file:
    line = line.rstrip() # remove trailing newline

    first_line = ''.join(chain.from_iterable(zip(line[0::4], line[1::4])))
    second_line = ''.join(chain.from_iterable(zip(line[2::4], line[3::4])))

    output_file.write(first_line   '\n')
    output_file.write(second_line   '\n')

This question uses methods described in the answers to this question and extends them to strings.

In essence, we use string slicing to read the first and second character into a tuple. Then, we skip to the fifth and sixth character, and read those into another tuple. We then chain all of these tuples into an iterable using chain.from_iterable() and then read it out into a string using ''.join().

We do the same for the second line, except we start at the third and fourth characters in the line rather than the first and second.

When in.txt is the file given, out.txt is:

soasiaok
eqhrbsxz
gajrxwgb
jqvtxiuy
iyzvflbd
hhjdwybx
cyqfcuuw
cnptknlt

CodePudding user response:

Using the chunks recipe from here:

def chunks(lst, n):
    """Yield successive n-sized chunks from lst."""
    for i in range(0, len(lst), n):
        yield lst[i:i   n]

chopped = [chunks(list(chunks(line, 2)), 2) for line in lines]
chopped = [zip(*c) for c in chopped]
for (row1parts, row2parts) in chopped:
    print(''.join(row1parts))
    print(''.join(row2parts))

For example:

lines = """
soeqashriabsokxz
gajqjrvtxwxigbuy
iyhhzvjdflwybdbx
cycnqfptcuknuwlt
""".strip().split()

def chunks(lst, n):
    for i in range(0, len(lst), n):
        yield lst[i:i   n]

chopped = [chunks(list(chunks(line, 2)), 2) for line in lines]
chopped = [zip(*c) for c in chopped]
for (row1parts, row2parts) in chopped:
    print(''.join(row1parts))
    print(''.join(row2parts))
soasiaok
eqhrbsxz
gajrxwgb
jqvtxiuy
iyzvflbd
hhjdwybx
cyqfcuuw
cnptknlt

CodePudding user response:

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
  • Related