Game Development Reference
In-Depth Information
You can get hold of an installer for Python from the following URL:
http://www.python.org/download/
The approach we will be using is to access the data from the text spreadsheet by
accessing it directly. If we save the spreadsheet in Excel 97 format (file extension
.xls , supported by most spreadsheet programs), there is an excellent Python library
called xlrd that can be downloaded here:
http://pypi.python.org/pypi/xlrd/
Install Python first and then install the xlrd library. It's a good idea to ensure that
the Python executable can be easily found by adding the Python install directory
to your path environment variable. An easy way to check if the Python directory is
already in your path variable is to open up a command prompt window and enter
path to display the current list of directories that will be searched.
To illustrate just how simple accessing data from a spreadsheet file is, using Python
and xlrd, the following script will open a spreadsheet file and output all the rows
and columns it contains:
import xlrd
lXLS = xlrd.open_workbook("StringList.xls")
lSheet = lXLS.sheet_by_index(0)
for lRow in range(lSheet.nrows):
lCells = lSheet.row_values(lRow, 0, lSheet.ncols)
print lCells
Even if you've never set eyes on a Python script before, this should be fairly easy to
follow, but here's a brief explanation.
The import xlrd line is equivalent to the #include directive in C/C++. It's just
stating that we want to make use of the xlrd library.
Next we open the spreadsheet file by calling the xlrd.open_workbook method,
passing in the filename of the spreadsheet we want to use. This returns an instance of a
Python class defined by xlrd that represents the spreadsheet file. Note that Python is a
loosely typed language and there is no need to declare what type the variable must be.
We call the sheet_by_index method on the spreadsheet object to retrieve the first
worksheet from the spreadsheet. This yields another Python object representing
the worksheet.
 
Search WWH ::




Custom Search