如何使用 SCRIPT 命令从 SPSS 语法 window 正确调用 Python 3 脚本(+ 运行时的其他问题)
How to properly invoke Python 3 script from SPSS syntax window using SCRIPT command (+ additional problems during runtime)
我想要 运行 两个 Python 3 来自 SPSS 语法的脚本 window。可以使用 BEGIN PROGRAM-END PROGRAM.
块或 SCRIPT
命令来执行它。这次我需要使用第二个命令找到解决方案。
简化代码:
*** MACROS.
define export_tabs (!positional !tokens (1))
output modify
/select logs headings texts warnings pagetitles outlineheaders notes
/deleteobject delete = yes.
OUTPUT EXPORT
/CONTENTS EXPORT = visible LAYERS = printsetting MODELVIEWS = printsetting
/XLSX DOCUMENTFILE = "doc.xlsx"
OPERATION = createsheet
sheet = !quote(!unquote(!1))
LOCATION = lastcolumn NOTESCAPTIONS = no
!enddefine.
define matrix_tab (!positional !charend('/')
/!positional !charend('/')
/!positional !charend('/')
/!positional !charend('/')
/stat = !tokens (1))
!do !i !in (!3)
ctables
/mrsets countduplicates = no
/vlabels variables = !concat(!1,_,!2,_,!i) display = label
/table !concat(!1,_,!2,_,!i)
[rowpct.responses.count !concat(!unquote(!stat),"40.0"), totals[count f40.0]]
/slabels position = column visible = no
/clabels rowlabels = opposite
/categories variables = !concat(!1,_,!2,_,!i) order = a key = value
empty = include total = yes label = "VALID COUNT" position = after
/titles title = !upcase(!4).
!doend
!enddefine.
*** REPORT.
* Sheet 1.
output close all.
matrix_tab $Q1 / 1 / 1 2 / "QUESTION 1" / stat="pct".
script "C:\path\script 1.py".
script "C:\path\script 2.py".
export_tabs "Q1".
* Sheet 2.
output close all.
matrix_tab $Q2 / 2 / 3 4 / "QUESTION 2" / stat="pct".
script "C:\path\script 1.py".
script "C:\path\script 2.py".
export_tabs "Q2".
当我 运行 第一个块 sheet 时一切正常。但是,当我 运行 第二个 sheet 的块时,SPSS 不执行 Python 脚本并直接跳转到 export_tabs
宏(同步问题?)。我认为我执行 SCRIPT
命令的方式可能存在问题。所以我尝试了这个:
script "C:\path\script 1.py" pythonversion = 3.
script "C:\path\script 2.py" pythonversion = 3.
但实际上 SPSS - 尽管语法 window 对语法的这些部分进行了着色 - 返回了此错误消息:
>Error # 3251 in column 152. Text: pythonversion
>The SCRIPT command contains unrecognized text following the the file
>specification. The optional parameter must be a quoted string enclosed in
>parentheses.
>Execution of this command stops.
有没有人遇到过这样的问题and/or知道为什么会这样?
NOTE:两个Python脚本运行顺利从Python3.4.3shell版本安装我有SPSS,所以我认为问题的核心不在那些代码中。
这似乎是此关键字实施方式中的文档缺陷。我已经能够复制它并使用 IBM SPSS Statistics Development 记录了一个缺陷。
在这种情况下,顺序很重要。而不是这个:
script "C:\path\script 2.py" pythonversion = 3.
试试看:
script pythonversion = 3 "C:\path\script 2.py".
我想要 运行 两个 Python 3 来自 SPSS 语法的脚本 window。可以使用 BEGIN PROGRAM-END PROGRAM.
块或 SCRIPT
命令来执行它。这次我需要使用第二个命令找到解决方案。
简化代码:
*** MACROS.
define export_tabs (!positional !tokens (1))
output modify
/select logs headings texts warnings pagetitles outlineheaders notes
/deleteobject delete = yes.
OUTPUT EXPORT
/CONTENTS EXPORT = visible LAYERS = printsetting MODELVIEWS = printsetting
/XLSX DOCUMENTFILE = "doc.xlsx"
OPERATION = createsheet
sheet = !quote(!unquote(!1))
LOCATION = lastcolumn NOTESCAPTIONS = no
!enddefine.
define matrix_tab (!positional !charend('/')
/!positional !charend('/')
/!positional !charend('/')
/!positional !charend('/')
/stat = !tokens (1))
!do !i !in (!3)
ctables
/mrsets countduplicates = no
/vlabels variables = !concat(!1,_,!2,_,!i) display = label
/table !concat(!1,_,!2,_,!i)
[rowpct.responses.count !concat(!unquote(!stat),"40.0"), totals[count f40.0]]
/slabels position = column visible = no
/clabels rowlabels = opposite
/categories variables = !concat(!1,_,!2,_,!i) order = a key = value
empty = include total = yes label = "VALID COUNT" position = after
/titles title = !upcase(!4).
!doend
!enddefine.
*** REPORT.
* Sheet 1.
output close all.
matrix_tab $Q1 / 1 / 1 2 / "QUESTION 1" / stat="pct".
script "C:\path\script 1.py".
script "C:\path\script 2.py".
export_tabs "Q1".
* Sheet 2.
output close all.
matrix_tab $Q2 / 2 / 3 4 / "QUESTION 2" / stat="pct".
script "C:\path\script 1.py".
script "C:\path\script 2.py".
export_tabs "Q2".
当我 运行 第一个块 sheet 时一切正常。但是,当我 运行 第二个 sheet 的块时,SPSS 不执行 Python 脚本并直接跳转到 export_tabs
宏(同步问题?)。我认为我执行 SCRIPT
命令的方式可能存在问题。所以我尝试了这个:
script "C:\path\script 1.py" pythonversion = 3.
script "C:\path\script 2.py" pythonversion = 3.
但实际上 SPSS - 尽管语法 window 对语法的这些部分进行了着色 - 返回了此错误消息:
>Error # 3251 in column 152. Text: pythonversion
>The SCRIPT command contains unrecognized text following the the file
>specification. The optional parameter must be a quoted string enclosed in
>parentheses.
>Execution of this command stops.
有没有人遇到过这样的问题and/or知道为什么会这样?
NOTE:两个Python脚本运行顺利从Python3.4.3shell版本安装我有SPSS,所以我认为问题的核心不在那些代码中。
这似乎是此关键字实施方式中的文档缺陷。我已经能够复制它并使用 IBM SPSS Statistics Development 记录了一个缺陷。
在这种情况下,顺序很重要。而不是这个:
script "C:\path\script 2.py" pythonversion = 3.
试试看:
script pythonversion = 3 "C:\path\script 2.py".