Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #34227

Re: CSV out of range

From Neil Cerutti <neilc@norwich.edu>
Newsgroups comp.lang.python
Subject Re: CSV out of range
Date 2012-12-04 13:58 +0000
Organization Norwich University
Message-ID <ai6dsiFk3vkU5@mid.individual.net> (permalink)
References <CAKhY55MFb6yTNu-nF-bAa0Fd0BCGyfd85d5FmCCDJ1CRL8nCCg@mail.gmail.com> <20121204113102.GA4194@taris.box> <CAKhY55MVEg=_RXo8gk8rAhWLmcrXs5nXb6+4rdZ1AoBBqVy1wQ@mail.gmail.com> <mailman.460.1354626169.29569.python-list@python.org>

Show all headers | View raw


On 2012-12-04, Anatoli Hristov <tolidtm@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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Re: CSV out of range Anatoli Hristov <tolidtm@gmail.com> - 2012-12-04 14:02 +0100
  Re: CSV out of range Neil Cerutti <neilc@norwich.edu> - 2012-12-04 13:58 +0000
    Re: CSV out of range Anatoli Hristov <tolidtm@gmail.com> - 2012-12-04 15:38 +0100

csiph-web