Python Syntax Rules
Within a Python program block, only statements and functions recognized by Python are allowed. Python syntax rules differ from IBM® SPSS® Statistics command syntax rules in a number of ways:
Python is case-sensitive. This includes variable names, function names, and pretty much anything
else you can think of. A variable name of myvariable is not the same as MyVariable, and the function spss.GetVariableCount
cannot be written as SPSS.getvariablecount
.
Python uses UNIX-style path specifications, with forward slashes. This applies even for IBM SPSS Statistics command syntax generated within a Python program block. For example:
spss.Submit("GET FILE '/data/somedata.sav'.")
Alternatively, you can escape each backslash with another backslash, as in:
spss.Submit("GET FILE '\\data\\somedata.sav'.")
There is no command terminator in Python, and continuation lines come in two flavors:
- Implicit. Expressions enclosed in parentheses, square brackets, or curly braces can continue across multiple lines without any continuation character. The expression continues implicitly until the closing character for the expression.
- Explicit. All other expression require a backslash at the end of each line to explicitly denote continuation.
Line indentation indicates grouping of statements. Groups of statements contained in conditional processing and looping structures are identified by indentation, as is the body of a user-defined Python function. There is no statement or character that indicates the end of the structure. Instead, the indentation level of the statements defines the structure, as in:
for i in xrange(varcount):
if spss.GetVariableMeasurementLevel(i)=="scale":
ScaleVarList=ScaleVarList + " " + spss.GetVariableName(i)
else:
CatVarList=CatVarList + " " + spss.GetVariableName(i)
print CatVarList
Note:
You should avoid the use of tab characters in Python code within BEGIN PROGRAM-END PROGRAM
blocks. For line
indentation, use spaces.