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. Advertising

  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. Advertising

  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. Advertising

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:
    966
    Antoon Pardon
    Jul 25, 2006
  2. Sacha Rook

    csv read clean up and write out to csv

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

    CSV out of range

    Anatoli Hristov, Dec 4, 2012, in forum: Python
    Replies:
    0
    Views:
    164
    Anatoli Hristov
    Dec 4, 2012
  4. Thomas Bach

    Re: CSV out of range

    Thomas Bach, Dec 4, 2012, in forum: Python
    Replies:
    0
    Views:
    124
    Thomas Bach
    Dec 4, 2012
  5. Anatoli Hristov

    Re: CSV out of range

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

Share This Page