fetchone Method (Python)
.fetchone().
Fetches the next row (case) from the active dataset. The result is a single tuple or the Python data type None after the last row has been read. A
value of None is also returned
at a split boundary. In this case, a subsequent call to fetchone
will retrieve the first case of
the next split group.
- This method is available in read or write mode.
- Each element in the returned tuple contains the data
value for a specific variable. The order of variable values in the
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 thespss.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
, orfetchmany
. If you need to retrieve all cases when weighting is in effect, then you can use the Dataset class. - The
fetchone
,fetchall
, andfetchmany
methods honor case filters specified with theFILTER
orUSE
commands.
DATA LIST FREE /var1 var2 var3.
BEGIN DATA
1 2 3
4 5 6
END DATA.
BEGIN PROGRAM.
import spss
dataCursor=spss.Cursor()
firstRow=dataCursor.fetchone()
secondRow=dataCursor.fetchone()
thirdRow=dataCursor.fetchone()
print("First row: ",firstRow)
print("Second row ",secondRow)
print("Third row...there is NO third row: ",thirdRow)
dataCursor.close()
END PROGRAM.
Result
First row: (1.0, 2.0, 3.0)
Second row (4.0, 5.0, 6.0)
Third row...there is NO third row: None