Closed Thread
Results 1 to 4 of 4

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

  1. #1
    Bertsche is offline Newbie
    Join Date
    Jun 2008
    Posts
    3
    Rep Power
    0

    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. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Bertsche is offline Newbie
    Join Date
    Jun 2008
    Posts
    3
    Rep Power
    0

    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]

  4. #3
    Luke Bradley is offline Newbie
    Join Date
    Jul 2008
    Posts
    6
    Rep Power
    0

    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)

  5. #4
    Bertsche is offline Newbie
    Join Date
    Jun 2008
    Posts
    3
    Rep Power
    0

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

    Thank you! Lists, got it.

Closed Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Access Database not displaying correctly on "Some" computers..
    By Chris Shipley in forum Visual Basic Programming
    Replies: 0
    Last Post: 06-20-2011, 07:34 PM
  2. cant understand " CSS : after pseudo-element "
    By alrazy1 in forum JavaScript and CSS
    Replies: 0
    Last Post: 01-11-2011, 06:48 AM
  3. AxWinsock Help VB 2010 "Property access must assign..."
    By CriticalError in forum Visual Basic Programming
    Replies: 0
    Last Post: 07-12-2010, 12:09 PM
  4. "ifs" and "elses" etc in python...help?!
    By amateur in forum Python
    Replies: 5
    Last Post: 07-10-2009, 01:21 PM
  5. Replies: 1
    Last Post: 04-23-2009, 06:50 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts