column selection

Xavier Ho contact at xavierho.com
Thu May 6 04:48:55 EDT 2010


You need to ignore empty lines.

for line in open('1.txt'):
    if len(line.strip()) == 0:
        continue
    columns = line.split()
    print columns[0], columns[2]

Cheers,
Xav

On Thu, May 6, 2010 at 6:22 PM, mannu jha <mannu_0523 at rediffmail.com> wrote:

> Hi,
>
> I have few files like this:
>
> 24 ALA helix (helix_alpha, helix2)
>
>
> 27 ALA helix (helix_alpha, helix2)
>
>
> 51 ALA helix (helix_alpha, helix4)
>
>
> 58 ALA helix (helix_alpha, helix5)
>
>
> 63 ALA helix (helix_alpha, helix5)
>
> now with this program:
>
> for line in open('1.txt'):
> columns = line.split()
> print columns[0], columns[2]
>
> I am trying to print only 1st and third column but it showing error like:
> mruser at caf:~> python split.py
> 24 helix
> Traceback (most recent call last):
> File "split.py", line 3, in
> print columns[0], columns[2]
> IndexError: list index out of range
> nmruser at caf:~>
> how to rectify it.
>
> Thanks,
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20100506/9a380082/attachment-0001.html>


More information about the Python-list mailing list