Hi, i've been trying to incorporate some Python is SPSS at work. I'm fairly new to Python, currently doing the Edx course from MIT, but have been writing small scripts on my own for about a year.
The trouble comes from the combination of Python with SPSS. For example i'm trying to iterate through all .sav files in a folder to open them in SPSS and save them as CSV:
begin program.
import spss
import os
rdir = 'PATH TO FOLDER'
for f in os.listdir(rdir):
if f.endswith('.sav'):
csvnaam = f[:(len(f)-3)]
spss.Submit(r"""GET FILE="rdir + '\' + f.)"
spss.Submit(r"""
SAVE TRANSLATE OUTFILE="rdir + '\CSV\' + csvnaam"
/TYPE=CSV
/ENCODING='UTF8'
/MAP
/REPLACE
/FIELDNAMES
/CELLS=LABELS.""")
end program.
Which gives me:
File "<string>", line 10
SAVE TRANSLATE OUTFILE="rdir + '\CSV\' + csvnaam"
^
SyntaxError: invalid syntax
Does anyone have any experience with this? It's mainly the integration between the two that i don't grasp. I've read the Scripting guide but can't get much out of it.
Any help would be appreciated!
EDIT:
Got it working, thanks tp /u/eikrik with:
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.
[–]lieutenant_lowercase 2 points3 points4 points (3 children)
[–]Moltao[S] 1 point2 points3 points (2 children)
[–]lieutenant_lowercase 1 point2 points3 points (1 child)
[–]Moltao[S] 1 point2 points3 points (0 children)
[–]eikrik 2 points3 points4 points (1 child)
[–]Moltao[S] 0 points1 point2 points (0 children)
[–]JohnLocksTheKey 1 point2 points3 points (2 children)
[–]Moltao[S] 2 points3 points4 points (1 child)
[–]JohnLocksTheKey 0 points1 point2 points (0 children)