0

hi all.........

a1 a2 a3 a4 a5 a6
b1 b2 b3 b4 b5 b6
c1 c2 c3 c4 c5 c6
d1 d2 d3 d4 d5 d6


i want to select a column and i need a output like

a1 a4
b1 b4
c1 c4
d1 d4

4
Contributors
4
Replies
5
Views
7 Years
Discussion Span
Last Post by snippsat
0
line = "a1 a2 a3 a4 a5 a6".split()

print line[0], line[3]
with open ("test.txt") as sis:
  for line in sis:
    split_line = line.strip().split()
    print "%s %s" % (split_line[0], split_line[3])
    
  sis.close()  

"""  
a1 a4
b1 b4
c1 c4
d1 d4
"""

Here: http://docs.python.org/ try to learn Python.

Edited by -ordi-: n/a

0

thanks for a reply............

but i m not getting output...wen i run a program it is showing error .

0

but i m not getting output...wen i run a program it is showing error .

Then you have to post the error we are not mind reader.
The code from -ordi- works fine,and with is a good thing to use because it close() file auto.
If you use python 3 change the print statement.

#Works python 3 and 2
with open ("test.txt") as sis:
  for line in sis:
    split_line = line.strip().split()
    print ("%s %s" % (split_line[0], split_line[3]))

A one liner for fun.

print '\n'.join([i[:2]+i[8:11] for i in open('test.txt')])

'''Out-->
a1 a4
b1 b4
c1 c4
d1 d4
'''

Edited by snippsat: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.