Date: Wed, 31 Dec 2008 16:39:56 -0600
Reply-To: "Peck, Jon" <peck@spss.com>
Sender: "SPSSX(r) Discussion" <SPSSX-L@LISTSERV.UGA.EDU>
From: "Peck, Jon" <peck@spss.com>
Subject: Re: if command line created in python is too long for spss
In-Reply-To: A<97D6F0A82A6E894DAF44B9F575305CC906D178AC@HCAMAIL03.ochca.com>
Content-Type: text/plain; charset="US-ASCII"
Python has no limit on the line length, but you are restricted to the SPSS line length limit when running a Python program within SPSS, including Submit text. Submit will do some wrapping, but the solutions below don't count on that.
You can break Python literals anywhere, joining with "+".
You can break your SPSS syntax anywhere, joining with "+". This gets hard to match up, though, so the easiest thing to do is to use Python triple quotes and build your SPSS syntax inside them, since the literals are automatically continued from line to line in Python this way.
Here's an example.
spss.Submit(r"""GET FILE=
' GET FILE='G:/Data/AMM/SPSS Files/' +
'first part of name' +
'second part of name'.""")
If recent_date is a variable that you need to substitute, the easiest way is to use the Python textwrap module to handle this. After an
import textwrap
use this code.
wrappedname = textwrap.wrap(recent_date) # breaks recent_date into 70-byte chunks and returns a list
recent_wrapped = "+\n".join(["'" + item + "'" for item in wrappedname]) # builds lines of SPSS literals
Then your submit would look like
spss.Submit(r"""GET FILE=
'G:/Data/AMM/SPSS Files/' +
%s.""" % recent_wrapped)
The recent_wrapped variable will be a string separated into lines with each line text in single quotes and with "+" signs in-between.
If the filename has single quotes in it, change the single quotes above into double quotes. The Python triple quotes will not be ended until a matching triple quote appears.
I know that's a bit much for New Year's Eve, but I hope it is clear enough.
Regards,
Jon Peck
-----Original Message-----
From: SPSSX(r) Discussion [mailto:SPSSX-L@LISTSERV.UGA.EDU] On Behalf Of Pirritano, Matthew
Sent: Wednesday, December 31, 2008 2:18 PM
To: SPSSX-L@LISTSERV.UGA.EDU
Subject: [SPSSX-L] if command line created in python is too long for spss
If have a situation where my filename is very long. If I try to open
this file from python I get the following error message.
>A text string is not correctly enclosed in quotation marks on the
command
>line. Literals may not be continued across command lines without the
use >of the continuation symbol '+'.
My get file statement in python looks like this:
spss.Submit("GET FILE='G:/Data/AMM/SPSS Files/" + recent_date + "'.")
Recent_date is the filename which is very long. So I'm assuming I need
to put a '+' somewhere in the filename. Wouldn't the '+' usually go at
the beginning of a new line? Forgive me, I don't use '+' in spss syntax
much. The question is, regardless of where the '+' should go in spss,
how do you know where to put it in python? Do I need to count characters
or something like that?
Thanks
Matt
Matthew Pirritano, Ph.D.
Research Analyst IV
Medical Services Initiative (MSI)
Orange County Health Care Agency
(714) 834-3566
=====================
To manage your subscription to SPSSX-L, send a message to
LISTSERV@LISTSERV.UGA.EDU (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
LISTSERV@LISTSERV.UGA.EDU (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