CSV out of range

Neil Cerutti neilc at norwich.edu
Tue Dec 4 08:58:42 EST 2012


On 2012-12-04, Anatoli Hristov <tolidtm at gmail.com> wrote:
> The issue is now solved I did:
>
> for x in mylist:
>     try:
>         sku.append(x[4])
>     except IndexError:
>         pass
>
> Thank you for your help

Optionally:

for x in mylist:
    if len(x) >= 4:
        sku.append(x[4])

But do you really need to save the whole file in a list first?
You could simply do:

for record in csvreader:
  if len(record) >= 4:
      sku.append(record[4])

Or even:

sku = [record[4] for record in csvreader if len(record) >= 4]

-- 
Neil Cerutti



More information about the Python-list mailing list