0

您好,我正在尝试将 Python 脚本转换为独立的 Python 脚本,但这不能按预期工作。输出发布到 Python 窗口,而不是普通的 SPSS 输出窗口 - 我该如何解决这个问题?

import spss
import SpssClient
import spssaux
from collections import defaultdict
import time
try:
    SpssClient.StartClient()
    SpssDataDoc=SpssClient.GetActiveDataDoc()

    print SpssDataDoc.GetDatasetName()
    print spssaux.GetActiveDatasetName()

    spssaux.spssaux.openDataFile("c:\bla.sav", filetype='sav')

    spss.spss.SetOutput("off")

    filterStr = """USE ALL.
    COMPUTE filter_$=({0}and category = {1}).
    FORMATS filter_$ (f1.0).
    FILTER BY filter_$.
    EXECUTE."""

    regressionStr ="""
    REGRESSION
      /DESCRIPTIVES MEAN STDDEV N
      /MISSING LISTWISE
      /STATISTICS COEFF OUTS R ANOVA COLLIN TOL
      /CRITERIA=PIN(.05) POUT(.10)
      /NOORIGIN 
      /DEPENDENT TotalLoyaltyScore
      /METHOD=ENTER var {0}
      /RESIDUALS DURBIN."""


      #initializing Lists
    CategoryDict = {1 : "Washing", 2: "dishwasher", 3: "Oven", 4:"Fridge", 5:"Vacuum"}
    CategoryVarDict = {1 : " recQ21r8 recQ21r9 recQ21r10 recQ21r11 recQ21r12 recQ21r13"}
    CategoryVarDict[2] = " recQ21r14 recQ21r15 recQ21r16 recQ21r17 recQ21r18 recQ21r19"
    CategoryVarDict[3] = " recQ21r20 recQ21r21 recQ21r22 recQ21r23 recQ21r24"
    CategoryVarDict[4] =" recQ21r25 recQ21r26 recQ21r27 recQ21r28 recQ21r29"
    CategoryVarDict[5] = " recQ21r30 recQ21r31 recQ21r32 recQ21r33 recQ21r34"

    VarDict = spssaux.spssaux.VariableDict()
    #vars = spssaux.spssaux.VariableDict().variables
    #for var in vars:
    #   print var

    ContinentVar = VarDict["Continent"]
    GenderVar = VarDict["Q25"]
    SchoolVar = VarDict["Q28"]
    AgeVar = VarDict["AgeEasy"]

    ContinentVL = spssaux.spssaux.GetValueLabels(ContinentVar)
    GenderVL = spssaux.spssaux.GetValueLabels(GenderVar)
    SchoolVL = spssaux.spssaux.GetValueLabels(SchoolVar)
    AgeVL = spssaux.spssaux.GetValueLabels(AgeVar)

    ToProcess = [ContinentVar, GenderVar, SchoolVar, AgeVar]
    SpssOutputDoc = SpssClient.GetDesignatedOutputDoc()

    for Category in CategoryDict:
        for Item in ToProcess:
            ValueLabel = spssaux.spssaux.GetValueLabels(Item)
            for Labels in ValueLabel:
                if not ValueLabel[Labels] == "no Answer":
                    SpssClient.LogToViewer("Category: " + CategoryDict[Category] + " Item: " + ValueLabel[Labels] +"\n")

                    # SpssOutputItem=SpssOutputDoc.CreateHeaderItem ("Category: " + CategoryDict[Category] + " Item: " + ValueLabel[Labels])
                    # itemlist = SpssOutputDoc.GetOutputItems()
                    # itemListLength = itemlist.Size()
                    # root = itemlist.GetItemAt(0).GetSpecificType()
                    # root.InsertChildItem(SpssOutputItem,root.GetChildCount())

                    spss.spss.Submit("title " + "" + "Category: " + CategoryDict[Category] + " Item: " + ValueLabel[Labels] + "" + "")
                    #Filtering
                    FirstPart = str(Item) + " =" + Labels + " "
                    realFilter = filterStr.format(FirstPart, Category)

                    #SpssClient.LogToViewer(realFilter)
                    spss.spss.Submit(realFilter.replace('\t',''))

                    #Regression
                    realRegression = regressionStr.format(CategoryVarDict[Category])
                    spss.spss.Submit(realRegression.replace('\t',''))

    OutputItems = SpssOutputDoc.GetOutputItems()
    for index in range(OutputItems.Size()):
        OutputItem = OutputItems.GetItemAt(index)
        if OutputItem.GetType() == SpssClient.OutputItemType.HEAD and OutputItem.GetDescription() == "Regression":
            TitleElem = OutputItems.GetItemAt(index-1)
            Spec = TitleElem.GetSpecificType()
            TitleText = Spec.GetTextContents()
            print(TitleText)


            OutputItem.SetDescription(TitleText)

            SItem = OutputItem.GetSpecificType()

            TitleChild = SItem.GetChildItem(0)
            STitleChild = TitleChild.GetSpecificType()
            STitleChild.SetTextContents(TitleText)
            # var title = Elem.GetChildItem(0)

            # title.SetTextContents(TitleText)
finally:
    SpssClient.StopClient()
4

1 回答 1

0

我假设您在外部模式下运行。您确定您在 Python 窗口中看到的不是从查看器文件中以纯文本形式回显的文本吗?如果你打电话

spss.SetOutput("关闭")

它将抑制输出回显到 Python。

于 2013-09-06T21:37:55.137 回答