csv - Python 2: IndexError: list index out of range -


i have "asin.txt" document:

in,huawei1,de out,huawei2,uk out,huawei3,none in,huawei4,fr in,huawei5,none in,huawei6,none out,huawei7,it 

i'm opening file , make ordereddict:

from collections import ordereddict reader = csv.reader(open('asin.txt','r'),delimiter=',') reader1 = csv.reader(open('asin.txt','r'),delimiter=',') d = ordereddict((row[0], row[1].strip()) row in reader) d1 = ordereddict((row[1], row[2].strip()) row in reader1) 

then want create variables (a,b,c,d) if take first line of asin.txt should like: a = in; b = huawei1; c = huawei1; d = de. i'm using "for" loop:

from itertools import izip (a, b), (c, d) in izip(d.items(), d1.items()): # here     try:       ....... 

it worked before, now, reason, prints error:

 d = ordereddict((row[0], row[1].strip()) row in reader)  indexerror: list index out of range 

how fix that?

probably have row in textfile not have @ least 2 fields delimited ",". e.g.:

in,huawei1 

try find solution along these lines:

d = ordereddict((row[0], row[1].strip()) row in reader if len(row) >= 2) 

or

l = [] row in reader:   if len(row) >= 2:     l.append(row[0], row[1].strip()) d = ordereddict(l) 

Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -