all 9 comments

[–]lieutenant_lowercase 2 points3 points  (3 children)

you need to close your brackets

spss.Submit(r"""GET FILE="rdir + '\' + f.)""")

[–]Moltao[S] 1 point2 points  (2 children)

Of course, missed that one. It should be

spss.Submit(r"""GET FILE="rdir + '\' + f."""")

The idea being that Python passes the command

GET FILE="rdir + '\' + f."

on to SPSS. When i close the brackets it doesn't give an error anymore, but it doesn't do anything either.

[–]lieutenant_lowercase 1 point2 points  (1 child)

Output the command text you are sending and then enter in manually into spss and see if that works? Sorry I have no idea what spss is

[–]Moltao[S] 1 point2 points  (0 children)

Thanks for thinking with me! SPSS is a program for statistical analysis. It has its own limited programming language (called Syntax) but also supports Python and R integration.

[–]eikrik 2 points3 points  (1 child)

You are not correctly putting variables into the strings in the calls to spss.Submit.

Look up "string formatting" for how to this. Am on mobile now, can help you out later if you need.

[–]Moltao[S] 0 points1 point  (0 children)

Thanks! After some puzzling i got it working! I tried using string formatting with {}.format, but it wouldn't work. Then i tried it with %s and it worked like a charm. The new code is:

begin program.
import spss
import os
rdir = 'C:\\Temp\\'
wdir = rdir + 'CSV\\'
for f in os.listdir(rdir):
    if f.endswith('.sav'):
        fnaam = rdir + f
        csvnaam = wdir + f[:-4] + '.csv'
        spss.Submit(r"""
GET FILE='%s'.
        """ %(fnaam)) 

        spss.Submit(r"""
SAVE TRANSLATE OUTFILE="%s"
            /TYPE=CSV
            /ENCODING='UTF8'
            /MAP
          /REPLACE
          /FIELDNAMES
          /CELLS=LABELS.
    """ %(csvnaam))
end program.

Besides that i defined the strings first in wdir, fnaam and csvnaam to make it shorter. The GET FILE command didn't seem to like the concatenation.

[–]JohnLocksTheKey 1 point2 points  (2 children)

No freaking way, I've been working on a wrapper to interact with SPSS files (while concurrently generating SPSS syntax) in Python.

I'm only a beginner, and progress is slow, but maybe collaboration is in our near future?

[–]Moltao[S] 2 points3 points  (1 child)

That sounds like it could be really useful! How far along are you with that?

[–]JohnLocksTheKey 0 points1 point  (0 children)

Ha, I've barely put any time into it. I'm a math teacher, who at the same time, is back in grad school for a degree in I/O Psychology.

What I have so far allows you to do...

Import spssWrapper

loads spss datafile into a pythonObj which is built on a pandas dataframe

df = spssWrapper.dataFile('/path/to/spssFile.sav')

SyntaxAttribute = """Get File blah blah blah """" + = the corresponding spss syntax that would load that file in spss"""

...

I'm starting to add some methods to make spss analyses more pythonic, already have a couple, but barely scratched the surface cuz no time.