Download Programming and Data Management for IBM SPSS Statistics 23: A

Document related concepts
no text concepts found
Transcript
v The argument pattern can be used with the variablesf method of VariableDict to create a list of
variables in the instance whose names match the associated regular expression. In this case, the regular
expression is the string ’.*\d$’.
v If you are not familiar with the syntax of regular expressions, a good introduction can be found in the
section "Regular expression operations" in the Python Library Reference, available at
http://docs.python.org/lib/module-re.html. Briefly, the character combination’.*’ will match an
arbitrary number of characters (other than a line break), and ’\d$’ will match a single digit at the end
of a string. The combination ’.*\d$’ will then match any string that ends in a digit. For an example
that uses a more complex regular expression, see .
Working with Case Data in the Active Dataset
The IBM SPSS Statistics - Integration Plug-in for Python provides the ability to read case data from the
active dataset, create new variables in the active dataset, and append new cases to the active dataset. This
is accomplished using methods from the Cursor class, available with the spss module. To concurrently
access multiple open datasets, use the Dataset class. See the topic “Creating and Accessing Multiple
Datasets” on page 187 for more information.
Using the Cursor Class
The Cursor class provides three usage modes: read mode allows you to read cases from the active
dataset, write mode allows you to add new variables (and their case values) to the active dataset, and
append mode allows you to append new cases to the active dataset. To use the Cursor class, you first
create an instance of the class and store it to a Python variable, as in:
dataCursor = spss.Cursor(accessType=’w’)
The optional argument accessType specifies the usage mode: read (’r’), write (’w’), or append (’a’). The
default is read mode. Each usage mode supports its own set of methods.
Note: For users of a 14.0.x version of the plug-in who are upgrading to version 15.0 or higher, read mode
is equivalent to the Cursor class provided with 14.0.x versions. No changes to your 14.0.x code for the
Cursor class are required to run the code with version 15.0 or higher.
Reading Case Data with the Cursor Class
To read case data, you create an instance of the Cursor class in read mode, as in:
dataCursor = spss.Cursor(accessType=’r’)
Read mode is the default mode, so specifying accessType=’r’ is optional. For example, the above is
equivalent to:
dataCursor = spss.Cursor()
Invoking Cursor with just the accessType argument, or no arguments, indicates that case data should be
retrieved for all variables in the active dataset.
You can also call Cursor with a list of index values for a set of specific variables to retrieve. Index values
represent position in the active dataset, starting with 0 for the first variable in file order. To illustrate this,
consider the variables in Employee data.sav and imagine that you want to retrieve case data for only the
variables id and salary, with index values of 0 and 5, respectively. The code to do this is:
dataCursor = spss.Cursor([0,5])
Note: When working with a subset of the variables in the active dataset, you may consider using the
spssdata module, which allows you to specify a list of variable names, rather than indexes. See the topic
“Using the spssdata Module” on page 176 for more information.
Example: Retrieving All Cases
Chapter 3. Programming with Python
167