Script/Macro/Syntax Help

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Script/Macro/Syntax Help

Craig Johnson
I am looking to create a script/macro/syntax to help me with some analyses.
In essence, I need to open a series of files, run syntax on each file, and
then save the altered file as FileName_R.sav.  For example say I have files
1.sav, 2.sav, and 3.sav in the folder C:/example/ and want to run a recode
on all the files using the following syntax file
C:/example/syntax/recode.sps.  The code needs to open up 1.sav, run
recode.sps, and then save the recoded file to 1_r.sav in C:/example/recode/.
 It would then open 2.sav, run recode.sps, and save the recoded file.  Finally,
it would open 3.sav, run recode.sps, and then save the recoded file.

The catch here is that the code needs to be generic so that it works on all
the files in the folder.  (Sorry merging them all together isn't an
option).  For example, the next time I run the syntax folder C:/example/ may
include 4.sav, 5.sav, and 6.sav so the code needs to account for that.

Any ideas on how to accomplish this using some sort of generic code?

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD
Reply | Threaded
Open this post in threaded view
|

Re: Script/Macro/Syntax Help

Albert-Jan Roskam
Hi!

This is similar to some problem posted earlier this week. See the code below. One general question re: python:
why is 'savlist' composed of elements of the form:
d:/temp\\somefile.sav
....whereas  'savlist[i]' generates something like
d:/temp\somefile.sav
Notice the single backslash. How can I prevent this? I tried several options mentioned in os.path, but without result.

Cheers!!
Albert-Jan

BEGIN PROGRAM.
import spss
import glob
savlist = glob.glob("d:/temp/*.sav")
if savlist:
        for i in range(len(savlist)):
                spss.Submit(""" get file = '%s'.
                                insert file = 'c:/example/recode.sps'.
                                save outfile = '%s_r.sav.'
                                """ % (savlist[i], savlist[i][:-4]))
else:
        print "Nothing to do"
END PROGRAM.


--- On Tue, 8/19/08, Craig Johnson <[hidden email]> wrote:

> From: Craig Johnson <[hidden email]>
> Subject: Script/Macro/Syntax Help
> To: [hidden email]
> Date: Tuesday, August 19, 2008, 7:59 PM
> I am looking to create a script/macro/syntax to help me with
> some analyses.
> In essence, I need to open a series of files, run syntax on
> each file, and
> then save the altered file as FileName_R.sav.  For example
> say I have files
> 1.sav, 2.sav, and 3.sav in the folder C:/example/ and want
> to run a recode
> on all the files using the following syntax file
> C:/example/syntax/recode.sps.  The code needs to open up
> 1.sav, run
> recode.sps, and then save the recoded file to 1_r.sav in
> C:/example/recode/.
>  It would then open 2.sav, run recode.sps, and save the
> recoded file.  Finally,
> it would open 3.sav, run recode.sps, and then save the
> recoded file.
>
> The catch here is that the code needs to be generic so that
> it works on all
> the files in the folder.  (Sorry merging them all together
> isn't an
> option).  For example, the next time I run the syntax
> folder C:/example/ may
> include 4.sav, 5.sav, and 6.sav so the code needs to
> account for that.
>
> Any ideas on how to accomplish this using some sort of
> generic code?
>
> =====================
> To manage your subscription to SPSSX-L, send a message to
> [hidden email] (not to SPSSX-L), with no body
> text except the
> command. To leave the list, send the command
> SIGNOFF SPSSX-L
> For a list of commands to manage subscriptions, send the
> command
> INFO REFCARD

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD
Reply | Threaded
Open this post in threaded view
|

Re: Script/Macro/Syntax Help

Peck, Jon
In Python strings, "\" is treated as an escape character, so, for example, "\t" is recognized as a tab character.  "\\" escapes the backslash and results in a single literal backslash character.

When writing a string literal, you can prefix it with r to indicate a "raw" string in which \ is treated literally, e.g., r"\temp" will be what it looks like.  Otherwise,
"\temp"
is tab followed by emp.

But this only applies while parsing a literal: it isn't an inherent property of strings.

When a string is displayed, it may be displayed in a safe format with the backslashes escaped.  That doesn't mean that they are actually present in the string.

Consider this little session:

>>> s = r"\temp"
>>> s
'\\temp'
>>> print(s)
\temp
>>> repr(s)
"'\\\\temp'"
>>> len(s)
5

>>> s="\temp"
>>> s
'\temp'
>>> print(s)
        emp
>>> repr(s)
"'\\temp'"
>>> len(s)
4

HTH,
Jon

-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of Albert-jan Roskam
Sent: Wednesday, August 20, 2008 3:51 AM
To: [hidden email]
Subject: Re: [SPSSX-L] Script/Macro/Syntax Help

Hi!

This is similar to some problem posted earlier this week. See the code below. One general question re: python:
why is 'savlist' composed of elements of the form:
d:/temp\\somefile.sav
....whereas  'savlist[i]' generates something like
d:/temp\somefile.sav
Notice the single backslash. How can I prevent this? I tried several options mentioned in os.path, but without result.

Cheers!!
Albert-Jan

BEGIN PROGRAM.
import spss
import glob
savlist = glob.glob("d:/temp/*.sav")
if savlist:
        for i in range(len(savlist)):
                spss.Submit(""" get file = '%s'.
                                insert file = 'c:/example/recode.sps'.
                                save outfile = '%s_r.sav.'
                                """ % (savlist[i], savlist[i][:-4]))
else:
        print "Nothing to do"
END PROGRAM.


--- On Tue, 8/19/08, Craig Johnson <[hidden email]> wrote:

> From: Craig Johnson <[hidden email]>
> Subject: Script/Macro/Syntax Help
> To: [hidden email]
> Date: Tuesday, August 19, 2008, 7:59 PM
> I am looking to create a script/macro/syntax to help me with
> some analyses.
> In essence, I need to open a series of files, run syntax on
> each file, and
> then save the altered file as FileName_R.sav.  For example
> say I have files
> 1.sav, 2.sav, and 3.sav in the folder C:/example/ and want
> to run a recode
> on all the files using the following syntax file
> C:/example/syntax/recode.sps.  The code needs to open up
> 1.sav, run
> recode.sps, and then save the recoded file to 1_r.sav in
> C:/example/recode/.
>  It would then open 2.sav, run recode.sps, and save the
> recoded file.  Finally,
> it would open 3.sav, run recode.sps, and then save the
> recoded file.
>
> The catch here is that the code needs to be generic so that
> it works on all
> the files in the folder.  (Sorry merging them all together
> isn't an
> option).  For example, the next time I run the syntax
> folder C:/example/ may
> include 4.sav, 5.sav, and 6.sav so the code needs to
> account for that.
>
> Any ideas on how to accomplish this using some sort of
> generic code?
>
> =====================
> To manage your subscription to SPSSX-L, send a message to
> [hidden email] (not to SPSSX-L), with no body
> text except the
> command. To leave the list, send the command
> SIGNOFF SPSSX-L
> For a list of commands to manage subscriptions, send the
> command
> INFO REFCARD

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD