Read Mode (Python)

This is the default for the Cursor class and provides the ability to read case data from the active dataset. Read mode is specified with spss.Cursor(accessType='r') or simply spss.Cursor().

Note: For users of a 14.0.x version of the plug-in who are upgrading to a newer version, this mode is equivalent to spss.Cursor(n) in 14.0.x versions. No changes to your 14.0.x code for the Cursor class are required to run the code with a newer version.

The Cursor methods fetchone, fetchmany, and fetchall are used to retrieve cases from the active dataset.

DATA LIST FREE /var1 (F) var2 (A2)  var3 (F).
BEGIN DATA
11 ab 13
21 cd 23
31 ef  33
END DATA.
BEGIN PROGRAM.
import spss
dataCursor=spss.Cursor()
oneRow=dataCursor.fetchone()
dataCursor.close()
i=[0]
dataCursor=spss.Cursor(i)
oneVar=dataCursor.fetchall()
dataCursor.close()
print "One row (case): ", oneRow
print "One column (variable): ", oneVar
END PROGRAM.

Result

One row (case):  (11.0, 'ab', 13.0)
One column (variable):  ((11.0,), (21.0,), (31.0,))
  • Cases from the active dataset are returned as a single tuple for fetchone and a list of tuples for fetchall.
  • Each tuple represents the data for one case. For fetchall the tuples are arranged in the same order as the cases in the active dataset.
  • Each element in a tuple contains the data value for a specific variable. The order of variable values within a tuple is the order specified by the optional argument var to the Cursor class, or file order if var is omittted.
*System- and user-missing values.
DATA LIST LIST (',') /numVar (f) stringVar (a4).
BEGIN DATA
1,a
,b
3,,
4,d
END DATA.
MISSING VALUES stringVar (' ').
BEGIN PROGRAM.
import spss
dataCursor=spss.Cursor()
print dataCursor.fetchall()
dataCursor.close()
END PROGRAM.

Result

((1.0, 'a   '), (None, 'b   '), (3.0, None), (4.0, 'd   '))
  • String values are right-padded to the defined width of the string variable.
  • System-missing values are always converted to the Python data type None.
  • By default, user-missing values are converted to the Python data type None. You can use the SetUserMissingInclude method to specify that user-missing values be treated as valid.