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

Recommended Answers

All 4 Replies

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.

You dont even need to worry about closing the file.
with does the magic.
;)

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

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

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
'''
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.