From: Gary Herron on 26 Apr 2010 11:14 mannu jha wrote: > Dear all, > > I am new in python, can anyone help me that how can I select two > column out of 6 column from a file. > For example if I have file like: > > a1 a2 a3 a4 a5 a6 > b1 b2 b3 b4 b5 b6 > c1 c2 c3 c4 c5 c6 > d1 d2 d3 d4 d5 d6 > > and I want output like: > > a1 a4 > b1 b4 > c1 c4 > d1 d4 > > then how to do > > Thanks Do you know how to open a file, and how to read individual lines from it? If so, then you can split a line at the spaces into a list by using fields = line.split() Then print fields[0], fields[3] would do what you ask. Gary Herron
From: J. Cliff Dyer on 26 Apr 2010 11:14 It depends on what you mean by a column. I assume your data is more complex than what you've shown us. If your data is really single words separated by spaces, you can do: for line in open('file'): columns = line.split() return columns[0], columns[3] If your columns can have spaces within them, or are separated in other ways, you'll need something else. Cheers, Cliff On Mon, 2010-04-26 at 14:50 +0000, mannu jha wrote: > Dear all, > > I am new in python, can anyone help me that how can I select two > column out of 6 column from a file. > For example if I have file like: > > a1 a2 a3 a4 a5 a6 > b1 b2 b3 b4 b5 b6 > c1 c2 c3 c4 c5 c6 > d1 d2 d3 d4 d5 d6 > > and I want output like: > > a1 a4 > b1 b4 > c1 c4 > d1 d4 > > then how to do > > Thanks
|
Pages: 1 Prev: PyDev : undefined variable from import Next: Python date time API |