From: Dave Angel on
mannu jha 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(a)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(a)caf:~>
> how to rectify it.
>
> Thanks,
>
>
If your files have two blank lines between each useful line, you have to
do something to avoid trying to print those items for the blank lines.
Depending how sure you are about your formatting, you could either do a
if not line: continue

or a
if columns < 3: continue

DaveA

From: Dave Angel on
mannu jha wrote:
> I tried with this:
>
> for line in open('1.txt'):
> columns = line.split()
> print columns[0], columns[1]
> if not line: continue
>
> but it is showing error:
>
> nmruser(a)caf:~> python split.py
> 24 ALA
> Traceback (most recent call last):
> File "split.py", line 3, in
> print columns[0], columns[1]
> IndexError: list index out of range
> nmruser(a)caf:~>
>
> Thanks,
>
> On Thu, 06 May 2010 15:44:07 +0530 wrote
>
> <snip>
>
> If your files have two blank lines between each useful line, you have to
>
> do something to avoid trying to print those items for the blank lines.
>
> Depending how sure you are about your formatting, you could either do a
>
> if not line: continue
>
> or a
>
> if columns < 3: continue
>
>
> DaveA
>
>
>
(Don't top-post. It makes the message very confusing to someone else
trying to follow it. Also, enable your mail program's quoting feature
-- currently it's not adding the marks at the beginning of each line you
quote.)

If you're going to skip over blank lines, it'd be good to do it before
trying to print from it. Move the test up by a line.

DaveA
From: Dave Angel on
Dave Angel wrote:
> <div class="moz-text-flowed" style="font-family: -moz-fixed">mannu jha
> wrote:
>> I tried with this:
>>
>> for line in open('1.txt'):
>> columns = line.split()
>> print columns[0], columns[1]
>> if not line: continue
>>
>> but it is showing error:
>>
>> nmruser(a)caf:~> python split.py
>> 24 ALA
>> Traceback (most recent call last):
>> File "split.py", line 3, in print columns[0], columns[1]
>> IndexError: list index out of range
>> nmruser(a)caf:~>
>>
>> Thanks,
>>
>> On Thu, 06 May 2010 15:44:07 +0530 wrote
>> <snip>
>>
>> If your files have two blank lines between each useful line, you have to
>> do something to avoid trying to print those items for the blank lines.
>> Depending how sure you are about your formatting, you could either do a
>>
>> if not line: continue
>>
>> or a
>>
>> if columns < 3: continue
>>
>>
>> DaveA
>>
>>
>>
> (Don't top-post. It makes the message very confusing to someone else
> trying to follow it. Also, enable your mail program's quoting feature
> -- currently it's not adding the marks at the beginning of each line
> you quote.)
>
> If you're going to skip over blank lines, it'd be good to do it before
> trying to print from it. Move the test up by a line.
>
> DaveA
>
for line in open('1.txt'):
columns = line.split()
if len(columns) < 2:
continue #skip over lines that
don't have at least 2 columns
print columns[0], columns[1]

DaveA