Michael Conlin
Michael Conlin

Reputation: 909

IndexError: string index out of range

Line 6 of this code produces a "IndexError: string index out of range". I assume that when I ask "if line[6] != None" there is no content in the line, but I can not figure out why. I have also tried using !='', but the problem is before that.

import csv
dict1={} 
list1 = csv.reader(open('014850_D_AA_20070207.txt'), delimiter='\t')
list1.next()    # skips the headers
for line in list1:
    if line[6] != None:
        dict1[line[0]]= (line[6] + ", " + line[11])
    else:
        continue    
print dict1

Upvotes: 1

Views: 2523

Answers (4)

Michael Conlin
Michael Conlin

Reputation: 909

Thanks for all the helpful ideas. I was able to solve my problem using the csv.DictReader as below:

import csv
dictPI = {}
dictGS = {}
fh = open('014850_D_AA_20070207.txt')
for line in csv.DictReader(fh, delimiter='\t'):
    ProbeID = line['ProbeID']
    GeneSymbol = line['GeneSymbol']
    Description = line['Description']
    if GeneSymbol != '':
        dictPI[ProbeID] = GeneSymbol, Description
        dictGS.setdefault(GeneSymbol, []).append(ProbeID)

Upvotes: 0

Arnaud Aliès
Arnaud Aliès

Reputation: 1092

to avoid that put that in a 'try' block and use string.find() for example

[-1] is for getting the last char in a string

also stuff like

else:
 continue

is unusual

I almost forgot to say: if string[5] != None: will raise a indexerror of course because if its none it will be out of range

Upvotes: 0

jimifiki
jimifiki

Reputation: 5534

Try to replace line 6 with:

if len(line) > 6:

but probaly

if len(line) > 11:

is better since you use line[11] below.

When you test line[6] to be == to None you already are trying to access the seventh element of line. This gives you an index error if the line contains less than 7 characters

Upvotes: 0

nneonneo
nneonneo

Reputation: 179412

  1. Python indexing is zero-based, so line[6] accesses the 7th element of line. In this case, line doesn't have seven elements, so it fails.

  2. if line[6] != None isn't the right way to check if a line has no content. Use if len(line) < 7 instead.

  3. csv.DictReader is a much nicer interface for reading CSV files with header rows. Iterating over it gives you the rows as dictionaries with the corresponding header items as keys, which makes the code a lot more understandable (compared to using magic indices).

Upvotes: 3

Related Questions