使用 Python 覆盖 SPSS 变量

Using Python to overwrite SPSS variable

我的 SPSS 数据集包含两个变量,名称分别为 time1time2,标签分别为 time1time2。最终,我想使用Python来读取变量并修改它们。作为一个简单的测试,我试图用 "hello" 替换 time1 的每个条目。这是我使用的 SPSS 语法:

begin program python.
import spss, spssdata

data = spssdata.Spssdata(indexes=["time1"], accessType="w")

for row in data:
    data.setvalue("time1", "hello")
    data.CommitCase()

data.CClose()
end program.

但我收到以下错误:

Warning: An open Cursor was detected while exiting a program block. The Cursor has been closed. Traceback (most recent call last):
File "", line 5, in File "C:\PROGRA~1\PREDIC~1\PSIMAG~1\IBM\SPSS\STATIS~1\PYTHON\Lib\site- packages\spssdata\spssdata.py", line 467, in setvalue index = [varspec[0] for varspec in self.newvars].index(var) ValueError: 'time1' is not in list

我在这里错过了什么?谢谢。

我的变量:

无法使用 spssdata 模块使用 Python 覆盖数据,因为它使用 spss 模块中的 Cursor 对象,无法覆盖变量。可以创建一个新变量并使用 Python 设置其值。然后您也可以删除旧变量并将新变量重命名为与旧变量相同的名称。

来自 IBM SPSS Statistics 23 的编程和数据管理:IBM SPSS Statistics 和 SAS 用户指南,第 167 页:

write mode allows you to add new variables (and their case values) to the active dataset