Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

[python] running macros in python, spss.Submit

324 views
Skip to first unread message

Lance Hoffmeyer

unread,
Feb 11, 2010, 1:32:38 PM2/11/10
to
Hey all,

Using python to zip a file. I typically have a macro at the top of my
syntax file that defines the working directory. I'm trying to run that
macro in python so I can zip a file. Can you run a macro through
spss.submit?

Another issue. I can create the zipfile if I put the entire path inside
the parens. But the zipfile is not compressed. It is the same size as
the original file.

If I create a python file (i.e. run.py) in the directory and run the
python script the zipfile is created and is compressed.

Is there a particular reason why running within the BEGIN PROGRAM PYTHON
section is preventing the zipfile.ZipFile command from compressing the
files. It should compress by default?

TIA,

Lance

Define !DATA() "C:\I use macros to define paths at top of spss syntax
file\" !enddefine.
BEGIN PROGRAM PYTHON.
import zipfile, spss
PATH = spss.Submit("!DATA")
print PATH
zipf = zipfile.ZipFile(PATH + "file.zip", mode="w")
zipf.write(!PATH + "file.xls")
zipf.close()
END PROGRAM.

JKPeck

unread,
Feb 11, 2010, 4:54:52 PM2/11/10
to
On Feb 11, 11:32 am, Lance Hoffmeyer <lance-n...@augustmail.com>
wrote:

Use spss.SetMacroValue to define a macro from within a Python program.

As for Zip compression, SPSS has nothing to do with that behavior, but
the zipfile.Zipfile constructor accepts a compression keyword. Take a
look at Zipfile help.

HTH,
Jon

Lance Hoffmeyer

unread,
Feb 11, 2010, 5:36:46 PM2/11/10
to
I don't want to create a macro within Python. I already already
created the macro. I want to run the macro from within python.

Lance

JKPeck

unread,
Feb 11, 2010, 9:47:58 PM2/11/10
to

In your example, !DATA is not a complete command. It seems to be a
location.

I ran the following successfully.
define !getit ()
GET FILE="C:/spss18/samples/english/cars.sav".
!enddefine.

BEGIN program.
import spss
spss.Submit("!getit")
end program.

I also ran this successfully,
define !getitloc () "C:/spss18/samples/english/cars.sav"
!enddefine.

BEGIN program.
import spss
spss.Submit("""GET FILE=!getitloc """)
end program.


However, I would suggest using file handles rather than macros to set
locations. These are much easier to work with than macros in this
situation. It is tricky to get the quotes, periods, and line breaks
right for macros for things like this.

HTH,
Jon

0 new messages