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
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]
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)
Thank you! Lists, got it.![]()
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks