+ Reply to Thread
Results 1 to 4 of 4

Thread: 2D Python Array - "List Indices Must Be Integers" (cannot access an array element)

  1. #1
    Newbie Bertsche is an unknown quantity at this point
    Join Date
    Jun 2008
    Posts
    3

    2D Python Array - "List Indices Must Be Integers" (cannot access an array element)

    Hi, I'm not very advanced in programming skills, but have found python to be a great scripting resource for automated tasks. Sorry if my post is elementary.

    I have figured out how to import an excel sheet (with xlrd add-on) into a 2D array of strings. Now I want to access each element of my array individually. I thought I could do that with myArray[0,2] after importing numpy, but I get this error: "List indices must be integers." how can I change this? I want to print out only certain elements of my array (only certain cells from my excel spreadsheet) like this: if (call a single cell) contains (the word "blah") print (that single cell). I can do all of it but the calling of a single cell, that is hanging me up.

    Here is my basic code (converting dates/times from excel omitted)

    import xlrd
    import numpy

    data = [] # Define array to place data in
    data = readData(sFile,data) # Read each line from file, return as 2D array

    print data[1] # Call one line of data. Works fine
    print data[0,2] # Attempt to call a single "cell." Fails.



    def readData(sFile,data):
    # Code adapted from [aspn.activestate.com/ASPN/Cookbook/Python/Recipe/546518]ASPN : Python Cookbook : Simple conversion of excel files into CSV and YAML[/url]

    book = xlrd.open_workbook(sFile)
    formatter = lambda(t,v): format_excelval(book,t,v,False)

    sheet_name = "Trademarks"

    raw_sheet = book.sheet_by_name(sheet_name)
    for row in range(raw_sheet.nrows):
    (types, values) = (raw_sheet.row_types(row), raw_sheet.row_values(row))
    data.append(map(formatter, zip(types, values)))

    return data

  2. #2
    Newbie Bertsche is an unknown quantity at this point
    Join Date
    Jun 2008
    Posts
    3

    Re: 2D Python Array - "List Indices Must Be Integers" (cannot access an array element

    I figured it out! For some reason my data is read in as a list; I can use numpy to convert that to an array. Like this:

    from numpy import *
    a = array( data )

    Then I can call a single element like this!
    print a[0,2]

  3. #3
    Newbie Luke Bradley is an unknown quantity at this point
    Join Date
    Jul 2008
    Posts
    6

    Re: 2D Python Array - "List Indices Must Be Integers" (cannot access an array element

    Good! I was gonna recommend numpy, but I also believe the way to access a 2d array in python would typically be
    print a[0][2]

    because a[0] is itself an array (we call them lists btw)

  4. #4
    Newbie Bertsche is an unknown quantity at this point
    Join Date
    Jun 2008
    Posts
    3

    Re: 2D Python Array - "List Indices Must Be Integers" (cannot access an array element

    Thank you! Lists, got it.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Similar Threads

  1. Python 2D array question
    By annannienann in forum Python
    Replies: 3
    Last Post: 04-23-2007, 01:36 PM
  2. Python Collection
    By reachpradeep in forum Python
    Replies: 1
    Last Post: 03-03-2007, 12:50 PM