Controlling encoding in spss.Submit in Python

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

Controlling encoding in spss.Submit in Python

Alex Huang
CONTENTS DELETED
The author has deleted this message.
Reply | Threaded
Open this post in threaded view
|

Re: Controlling encoding in spss.Submit in Python

Jon Peck
This is a little tricky as your program is going from the syntax window, which is in utf-16 through the Statistics backend, which could be in Unicode utf-8 mode or in code page mode and on to the Python interpreter.  And Python 2.x expects ascii input unless another encoding is declared in the first two line of the Python code.  This is the darkest corner of Python 2 (fortunately cleaned up in Python 3, but that won't help you right now.)  For starters, be sure that Statistics is in Unicode mode.  I probably won't have time to sort this out until tomorrow.  Also, what version of Statistics are you using?

On Fri, Jan 15, 2016 at 12:37 AM, Alex Huang <[hidden email]> wrote:
Dear List,

Hi. I would like to submit a string (syntax more than 256 characters) to
SPSS by spss.Submit. While I am doing this, there are some utf-8-without BOM
characters in the syntax. I would like SPSS to take it by refering the
encoding to SPSS. Here is briefly what I did:

BEGIN PROGRAM.
import spss
import codecs
exist = 0
for i in range(spss.GetVariableCount()):
    name=spss.GetVariableName(i)
    if name == "age2_18plus":
        exist = 1
if (exist):
    spss.Submit(
          codecs.encode("""VALUE LABELS age2_18plus
                        -99 '拒絕回答'
                        1 '18-19歲'
                        2 '20-24歲'
                        3 '25-29歲'
                        4 '30-34歲'
                        5 '35-39歲'
                        6 '40-44歲'
                        7 '45-49歲'
                        8 '50-54歲'
                        9 '55-59歲'
                        10 '60-64歲'
                        11 '65-69歲'
                        12 '70歲或以上'/
                    .""", "utf_8_sig"))
END PROGRAM.

I get this error:
>Error # 1.  Command name: VALUE
>The first word in the line is not recognized as an SPSS Statistics command.
>Execution of this command stops.

I used triple quote while codecs.encode is not accepting. How should I
change the code in order?

Thank you very much,
Alex







--
View this message in context: http://spssx-discussion.1045642.n5.nabble.com/Controlling-encoding-in-spss-Submit-in-Python-tp5731289.html
Sent from the SPSSX Discussion mailing list archive at Nabble.com.

=====================
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



--
Jon K Peck
[hidden email]

===================== 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: Controlling encoding in spss.Submit in Python

Jon Peck
In reply to this post by Alex Huang
You should be letting Statistics handle the encoding conversion.  Assuming Unicode mode, this will work unless, perhaps, you have a very old version of Statistics.  All I did was to declare the literal as a Unicode string and remove the encoding call.

BEGIN PROGRAM.
import spss
import codecs
exist = 0
for i in range(spss.GetVariableCount()):
    name=spss.GetVariableName(i)
    if name == "age2_18plus":
        exist = 1
if (exist):
    spss.Submit(
u"""VALUE LABELS age2_18plus
                        -99 '拒絕回答'
                        1 '18-19歲'
                        2 '20-24歲'
                        3 '25-29歲'
                        4 '30-34歲'
                        5 '35-39歲'
                        6 '40-44歲'
                        7 '45-49歲'
                        8 '50-54歲'
                        9 '55-59歲'
                        10 '60-64歲'
                        11 '65-69歲'
                        12 '70歲或以上'/
                    .""")
END PROGRAM.

On Fri, Jan 15, 2016 at 12:37 AM, Alex Huang <[hidden email]> wrote:
Dear List,

Hi. I would like to submit a string (syntax more than 256 characters) to
SPSS by spss.Submit. While I am doing this, there are some utf-8-without BOM
characters in the syntax. I would like SPSS to take it by refering the
encoding to SPSS. Here is briefly what I did:

BEGIN PROGRAM.
import spss
import codecs
exist = 0
for i in range(spss.GetVariableCount()):
    name=spss.GetVariableName(i)
    if name == "age2_18plus":
        exist = 1
if (exist):
    spss.Submit(
          codecs.encode("""VALUE LABELS age2_18plus
                        -99 '拒絕回答'
                        1 '18-19歲'
                        2 '20-24歲'
                        3 '25-29歲'
                        4 '30-34歲'
                        5 '35-39歲'
                        6 '40-44歲'
                        7 '45-49歲'
                        8 '50-54歲'
                        9 '55-59歲'
                        10 '60-64歲'
                        11 '65-69歲'
                        12 '70歲或以上'/
                    .""", "utf_8_sig"))
END PROGRAM.

I get this error:
>Error # 1.  Command name: VALUE
>The first word in the line is not recognized as an SPSS Statistics command.
>Execution of this command stops.

I used triple quote while codecs.encode is not accepting. How should I
change the code in order?

Thank you very much,
Alex







--
View this message in context: http://spssx-discussion.1045642.n5.nabble.com/Controlling-encoding-in-spss-Submit-in-Python-tp5731289.html
Sent from the SPSSX Discussion mailing list archive at Nabble.com.

=====================
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



--
Jon K Peck
[hidden email]

===================== 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: Controlling encoding in spss.Submit in Python

Alex Huang
CONTENTS DELETED
The author has deleted this message.