I need to direct the output of Python's print() to various output documents. The print statements exist in a series of separate syntax files. These files are called one after the other with INSERT FILE = <path>.
Currently I am using custom python functions to switch the designated output doc. This is not working under all circumstances. Please see the comments in the syntax below.
In layman's terms, the Python code appears to "overtake itself" during execution.
How can I ensure that the prints are routed correctly?
The real use case relates not only to print(), but also to the output of regression analyses, etc.
* Encoding: UTF-8.
BEGIN PROGRAM PYTHON3.
import spss, SpssClient
SpssClient.StartClient()
out_a = SpssClient.NewOutputDoc()
spss.Submit ('OUTPUT NAME A')
out_b = SpssClient.NewOutputDoc()
SpssClient.StopClient()
spss.Submit ('OUTPUT NAME B')
def print_a (msg):
# prints to out_a
SpssClient.StartClient()
out_a.SetAsDesignatedOutputDoc()
SpssClient.StopClient
print(msg)
def print_b (msg):
# prints to out_b
SpssClient.StartClient()
out_b.SetAsDesignatedOutputDoc()
SpssClient.StopClient
print(msg)
END PROGRAM.
* ------------------------------------.
* This prints everything to out_b :( .
BEGIN PROGRAM PYTHON3.
print_a ('A 1')
print_b ('B 1')
print_a ('A 2')
print_b ('B 2')
END PROGRAM.
* ------------------------------------.
* This switches between outputs as expected :) .
BEGIN PROGRAM PYTHON3.
print_a ('A 3')
END PROGRAM.
BEGIN PROGRAM PYTHON3.
print_b ('B 3')
END PROGRAM.
BEGIN PROGRAM PYTHON3.
print_a ('A 4')
END PROGRAM.
BEGIN PROGRAM PYTHON3.
print_b ('B 4')
END PROGRAM.
* ------------------------------------.
* This, however, keeps printing to (the previously designated) out_b :( .
INSERT FILE = '/Path/to/inserted.sps'.
* Given inserted.sps contains
* BEGIN PROGRAM PYTHON3.
* print_a ('A 5')
END PROGRAM.
* BEGIN PROGRAM PYTHON3.
* print_b ('B 5')
END PROGRAM.
* BEGIN PROGRAM PYTHON3.
* print_a ('A 6')
END PROGRAM.
* BEGIN PROGRAM PYTHON3.
* print_b ('B 6')
END PROGRAM.
------------------------------
Frank Watzl
------------------------------
#SPSSStatistics