fetchmany Method (Python)

.fetchmany(n). Fetches the next n cases from the active dataset, where n is a positive integer. If the value of n is greater than the number of remaining cases (and the dataset does not contain splits), it returns the value of all the remaining cases. In the case that the active dataset has splits, if n is greater than the number of remaining cases in the current split, it returns the value of the remaining cases in the split. If there are no remaining cases, the result is an empty tuple.

  • This method is available in read or write mode.
  • When used in write mode, calling fetchmany(n) will position the record pointer at case n of the active dataset. In the case that the dataset has splits and n is greater than the number of remaining cases in the current split, fetchmany(n) will position the record pointer at the end of the current split.
  • Cases from the active dataset are returned as a list of tuples. Each tuple represents the data for one case, and 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 variable index values in the optional argument n to the Cursor class, or file order if n is omitted. For example, if n=[5,2,7] the first tuple element is the value of the variable with index value 5, the second is the variable with index value 2, and the third is the variable with index value 7.
  • 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.
  • Values of variables with time formats are returned as integers representing the number of seconds from midnight.
  • By default, values of variables with date or datetime formats are returned as integers representing the number of seconds from October 14, 1582. You can specify to convert values of those variables to Python datetime.datetime objects with the cvtDates argument to the spss.Cursor function. See the topic spss.Cursor Class (Python) for more information.
  • If a weight variable has been defined for the active dataset, then cases with zero, negative, or missing values for the weighting variable are skipped when fetching data with fetchone, fetchall, or fetchmany. If you need to retrieve all cases when weighting is in effect, then you can use the Dataset class.
  • The fetchone, fetchall, and fetchmany methods honor case filters specified with the FILTER or USE commands.
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()
n=2
print dataCursor.fetchmany(n)
print dataCursor.fetchmany(n)
print dataCursor.fetchmany(n)
dataCursor.close()
END PROGRAM.

Result

((11.0, 'ab', 13.0), (21.0, 'cd', 23.0))
((31.0, 'ef', 33.0),)
()