1

I have file.txt containing two lines :

this is one
and my pen

Output should be like print each colum of every line in single row :

tahnids imsy opneen

How can we print this output in Python?

I tried the following but I'm stuck on jumping between alternate characters in each line. I'm looking for a generic solution whether one line or two line or more.

file=open('file.txt','r')
list1=[x.rstrip('\n') for x in file]
for i in list1:
    n=len(i)
    c=0
    while c<n:
        print(i[c],end=" ")
        c=c+1
    break

This it is printing only "ta".

tripleee
  • 175,061
  • 34
  • 275
  • 318
ankur-AJ
  • 31
  • 6
  • The `zip` function in Python lets you pair up items from two lists. If you still need help, you should [edit] your question, rather than heap on comments which nobody will see. – tripleee Jul 23 '19 at 15:29
  • I attempted to update your question but I had to guess your indentation. Please review and edit further if necessary. – tripleee Jul 23 '19 at 15:35
  • Why do you `break`? I'm guessing the indentation on that is wrong actually? – tripleee Jul 23 '19 at 15:43
  • How should the program behave if the lines are not all the same length? – tripleee Jul 23 '19 at 15:44

1 Answers1

3

It's debatable whether oneliners are good for this sort of thing but itertools can do this.

>>> from itertools import chain
>>> with open('/path/to/file') as data:
...     # could be just data.readlines() if you don't mind the newlines
...     a, b = [l.strip() for l in data.readlines()]
>>> # a = "this is one"                                                                                                                  
>>> # b = "and my pen"
>>> ''.join(chain.from_iterable(zip(a, b))
'tahnids  miys  poenn'

I'm also not sure your expected result is correct. If you're alternating all the characters, the two spaces should be together.

If your file has more than two lines, replacing a, b = ... with lines = ... and then using zip(*lines) should work for any number.

If you want to avoid itertools

''.join(''.join(x) for x in zip(a, b))

To include all the characters, even when the lines are not the same length, you can use itertools again.

from itertools import chain, zip_longest
''.join(chain.from_iterable(zip_longest(a, b, fillvalue='')))
# or
''.join(chain.from_iterable(zip_longest(*lines, fillvalue='')))
Holloway
  • 6,412
  • 1
  • 26
  • 33
  • can this be without itertools module. `b="" for x,y in zip(s1,s2): b += x+y print(b) ` . but problem is if lines are not equal in lengh zip will no pick that pair; so either fill line with zeros for missing characters or spaces. – ankur-AJ Jul 23 '19 at 17:05
  • @tech_guy probably but itertools is part of the standard library so why would you? – Holloway Jul 23 '19 at 17:06
  • Added alternative – Holloway Jul 23 '19 at 17:09
  • oh nice so join function itself extrac all values from generator object @holloway – ankur-AJ Jul 23 '19 at 17:15
  • The inner join joins each pair of characters. The outer join then joins the pairs. – Holloway Jul 23 '19 at 17:20
  • If the lines aren't equal length, what behaviour would you like? – Holloway Jul 23 '19 at 17:21
  • if not equal length it should print all characters of lines. That is what zip is not taking care – ankur-AJ Jul 23 '19 at 17:29
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/196884/discussion-between-tech-guy-and-holloway). – ankur-AJ Jul 23 '19 at 17:31
  • If your file has more than two lines, replacing a, b = ... with lines = ... and then using zip(lines) should work for any number. ` lines = [l.strip() for l in data.readlines()] print(''.join(chain.from_iterable(zip(lines))))` . not working as expected – ankur-AJ Jul 24 '19 at 04:32