0

I am trying to use split in python to iterate through columns but when trying to access columns I get an error that a particular list index is out of range.

This is what my file looks like:

chr1    15585711    .   A   T   45.7081 .   AB=0;ABP=0;AC=2;AF=1;AN=2;AO=3;CIGAR=1X;DP=3;DPB=3;DPRA=0;EPP=9.52472;EPPR=0;GTI=0;LEN=1;MEANALT=1;MQM=60;MQMR=0;        NS=1;NUMALT=1;ODDS=8.76405;PAIRED=0;PAIREDR=0;PAO=0;PQA=0;PQR=0;PRO=0;QA=78;QR=0;RO=0;RPP=9.52472;RPPR=0;RUN=1;SAF=0;SAP=9.52472;SAR=3;SRF=0;SRP=0;SRR=0;            TYPE=snp  GT:DP:RO:QR:AO:QA:GL    1/1:3:0:0:3:78:-7.28,-0.90309,0

And here is my code:

target = open('file', 'r')

for line in target:
    line = line.split('\t')
    print line[1]

When I print line it looks like a list to me, and line[0] does access chr1 which is the first column. But then when I try to access the other columns i cannot.

10
  • 2
    Are you sure its TAB separated ('\t')? It does not seem like that. Try line.split() to split on every whitespace Commented Jan 25, 2016 at 19:45
  • 1
    line[0] does access chr1 <- you're asking for line[1] in your code, though Commented Jan 25, 2016 at 19:46
  • 2
    What do you get with print line.split('\t') Commented Jan 25, 2016 at 19:46
  • It should be tab separated. And @Lafexlos I get the following: ['chr1', '15585711', '.', 'A', 'T', '45.7081', '.', 'AB=0;ABP=0;AC=2;AF=1;AN=2;AO=3;CIGAR=1X;DP=3;DPB=3;DPRA=0;EPP=9.52472;EPPR=0;GTI=0;LEN=1;MEANALT=1;MQM=60;MQMR=0;NS=1;NUMALT=1;ODDS=8.76405;PAIRED=0;PAIREDR=0;PAO=0;PQA=0;PQR=0;PRO=0;QA=78;QR=0;RO=0;RPP=9.52472;RPPR=0;RUN=1;SAF=0;SAP=9.52472;SAR=3;SRF=0;SRP=0;SRR=0;TYPE=snp', 'GT:DP:RO:QR:AO:QA:GL', '1/1:3:0:0:3:78:-7.28,-0.90309,0\n'] Commented Jan 25, 2016 at 19:47
  • 2
    You are reusing the variable line and changing it. It probably works the first time but the next time through you've already split it on tab... only one entry in that case. Commented Jan 25, 2016 at 19:47

2 Answers 2

1

I just created a text file with data you provided and was able to split it without any problem. I would suggest to use with open it'll close your file for you when you're done:

with open('test', 'rb') as target:
    for line in target:
        print line.split()[1]

Output:

15585711
Sign up to request clarification or add additional context in comments.

Comments

1

The source is not using tabs. Just use:

line = line.split()

1 Comment

when I try this I still get the same error. However if I just paste the text into a variable and then split I do not get an error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.