Re: CSV out of range

Discussion in 'Python' started by Anatoli Hristov, Dec 4, 2012.

  1. The issue is now solved I did:

    for x in mylist:
    try:
    sku.append(x[4])
    except IndexError:
    pass

    Thank you for your help

    Anatoli
     
    Anatoli Hristov, Dec 4, 2012
    #1
    1. Advertisements

  2. Anatoli Hristov

    Neil Cerutti Guest

    On 2012-12-04, Anatoli Hristov <> 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
     
    Neil Cerutti, Dec 4, 2012
    #2
    1. Advertisements

  3. On Tue, Dec 4, 2012 at 2:58 PM, Neil Cerutti <> wrote:
    > On 2012-12-04, Anatoli Hristov <> 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


    Thanks Neil,

    I'm still testing it - just trying to clean the things out and be sure
    that I can do all of the stuff :)

    I will create a list only of the products I have in the DB and will
    compare them for prices stock etc... so the list will be smaller :)

    Thanks again

    Anatoli
     
    Anatoli Hristov, Dec 4, 2012
    #3
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    46
    Views:
    1,356
    Antoon Pardon
    Jul 25, 2006
  2. Lambda
    Replies:
    2
    Views:
    594
    James Kanze
    Jul 16, 2008
  3. Tomoyuki Kosimizu

    Range does not take an Range object.

    Tomoyuki Kosimizu, Nov 25, 2003, in forum: Ruby
    Replies:
    3
    Views:
    290
    Tomoyuki Kosimizu
    Nov 27, 2003
  4. David Bird
    Replies:
    1
    Views:
    502
    Tiago Macedo
    Jun 23, 2008
  5. Joey Zhou
    Replies:
    5
    Views:
    448
    Joey Zhou
    Apr 15, 2011
  6. it_says_BALLS_on_your forehead

    extract range of lines using range op bug?

    it_says_BALLS_on_your forehead, Mar 3, 2006, in forum: Perl Misc
    Replies:
    3
    Views:
    361
    it_says_BALLS_on_your forehead
    Mar 3, 2006
  7. Sacha Rook

    csv read clean up and write out to csv

    Sacha Rook, Nov 2, 2012, in forum: Python
    Replies:
    2
    Views:
    486
    Hans Mulder
    Nov 2, 2012
  8. Anatoli Hristov

    CSV out of range

    Anatoli Hristov, Dec 4, 2012, in forum: Python
    Replies:
    0
    Views:
    264
    Anatoli Hristov
    Dec 4, 2012
Loading...