简体   繁体   English

使用Python覆盖SPSS变量

[英]Using Python to overwrite SPSS variable

My SPSS dataset contains two variables with names time1 and time2 and labels time1 and time2 respectively. 我的SPSS数据集包含两个名为time1time2变量,分别标记为time1time2 Eventually, I want to use Python to read the variables and modify them. 最终,我想使用Python读取变量并进行修改。 As a simple test, I am trying to replace every entry of time1 with "hello" . 作为一个简单的测试,我试图用"hello"替换time1每个条目。 This is the SPSS syntax I am using: 这是我正在使用的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.

But I am getting the following error: 但是我收到以下错误:

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\\5\\IBM\\SPSS\\STATIS~1\\25\\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 文件“ C:\\ PROGRA〜1 \\ PREDIC〜1 \\ PSIMAG〜1 \\ 5 \\ IBM \\ SPSS \\ STATIS〜1 \\ 25 \\ PYTHON \\ Lib \\ site-packages \\ spssdata \\ spssdata.py中的文件”“,第5行“,第467行,在setvalue索引中= [varspec [0]对于self.newvars中的varspec]。index(var)ValueError:'time1'不在列表中

What am I missing here? 我在这里想念什么? Thank you. 谢谢。

My variables: 我的变量:

我的变量

It is not possible to overwrite data using Python using the spssdata module becuase it uses the Cursor object from the spss module, which cannot overwrite variables. 无法使用spssdata模块使用Python覆盖数据,因为它使用了spss模块中的Cursor对象,该对象无法覆盖变量。 It is possible to create a new variable and set its values using Python. 可以使用Python创建一个新变量并设置其值。 You can then also delete the old variable and rename the new variable to the same name as the old one. 然后,您也可以删除旧变量,然后将新变量重命名为与旧变量相同的名称。

From Programming and Data Management for IBM SPSS Statistics 23: A Guide for IBM SPSS Statistics and SAS Users , page 167: 摘自《 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 写入模式允许您将新变量(及其大小写值)添加到活动数据集中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM