Scan CSV file and saving it into an array

Discussion in 'Python' started by Ana Dionísio, Apr 25, 2013.

  1. Hello!

    I have this script that scans a csv file and if the value in the first column == 200 it saves that row into an array.

    The problem is, I need to save that row and the next 10 rows in that same array. What can I add to the script so it does that? I tried to do for row in len(10): but I get an error.


    p = csv.reader(open('file.csv'), delimiter=';')
    a=[0]*2881
    a = numpy.array(a, dtype=dict)
    for row in p:
    if row[0]=="200":
    a=row
    break
    print a
    Ana Dionísio, Apr 25, 2013
    #1
    1. Advertising

  2. Ana Dionísio

    Dave Angel Guest

    On 04/24/2013 07:01 PM, Ana Dionísio wrote:
    > Hello!
    >
    > I have this script that scans a csv file and if the value in the first column == 200 it saves that row into an array.


    No it doesn't. It creates a list, then overwrites it with a numpy array,
    then overwrites that with a list of strings representing one row.

    If you want to really use a Python array, then read here:

    http://docs.python.org/2/library/array.html

    It'd probably be best to start with a precise problem statement,
    presumably copied from your textbook or assignment sheet. What python
    version is this for? And what OS? (that affects whether you need a
    file mode) Exactly what data structure are you trying to build? What
    type of a csv file are you trying to use? Is there a standard header
    line? How big might the file be? What behavior do you want if there's
    no line that begins with the field "200"? Or if there's more than one
    such line? Or if there are less than 10 lines following it in the file?
    What about a field of "0200"?

    >
    > The problem is, I need to save that row and the next 10 rows in that same array. What can I add to the script so it does that? I tried to do for row in len(10): but I get an error.
    >


    When you say "get an error" it could be one of many things. In this
    case, it's obvious, since len() doesn't make sense with an integer
    parameter. But in general you want to say either:

    1) it gave me the wrong result. I expected AAAA and got BBBB
    2) it did nothing at all.
    3) it gave an exception, and here's the full traceback.


    >
    > p = csv.reader(open('file.csv'), delimiter=';')
    > a=[0]*2881
    > a = numpy.array(a, dtype=dict)


    These two lines do nothing useful, and they confuse the reader of the
    code, since they imply that the list will end up of size 2881, and/or as
    a numpy array.

    > for row in p:
    > if row[0]=="200":
    > a=row
    > break


    missing else clause. How do you detect that there was no match?

    > print a
    >



    --
    DaveA
    Dave Angel, Apr 25, 2013
    #2
    1. Advertising

  3. On 25 April 2013 00:01, Ana Dionísio <> wrote:
    > Hello!
    >
    > I have this script that scans a csv file and if the value in the first column == 200 it saves that row into an array.
    >
    > The problem is, I need to save that row and the next 10 rows in that samearray. What can I add to the script so it does that? I tried to do for rowin len(10): but I get an error.
    >
    >
    > p = csv.reader(open('file.csv'), delimiter=';')
    > a=[0]*2881
    > a = numpy.array(a, dtype=dict)


    You shouldn't be using a numpy array for this; use a list instead. I
    suggest that you have a go at the Python tutorial and avoid using
    numpy until you are more confident with the basics of Python itself.

    The tutorial (for Python 2) is here:
    http://docs.python.org/2/tutorial/


    Oscar
    Oscar Benjamin, Apr 25, 2013
    #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. Luis Esteban Valencia
    Replies:
    0
    Views:
    2,490
    Luis Esteban Valencia
    Jan 6, 2005
  2. =?Utf-8?B?c2FtZWVy?=

    virus scan documents before saving to database

    =?Utf-8?B?c2FtZWVy?=, Oct 18, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    599
    =?Utf-8?B?c2FtZWVy?=
    Oct 18, 2006
  3. Tintin92
    Replies:
    1
    Views:
    1,672
    Andrew Thompson
    Feb 14, 2007
  4. jliu66
    Replies:
    0
    Views:
    492
    jliu66
    Oct 19, 2007
  5. Martin Foster
    Replies:
    4
    Views:
    145
    Martin Foster
    Dec 8, 2003
Loading...

Share This Page