Python string size problem

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

Python string size problem

Mario Giesel
Hello, Pythoneers,
 
I'm experimenting with a string of variables that is used as an argument of a function.
The string is a bit long, however (some 500 characters).
When I run the program I get this error message:
 
>Error # 6892.  Command name: BEGIN PROGRAM
>The string's length is more than 251.
>This command not executed.
>Error # 1.  Command name: "Coded_1113
>The first word in the line is not recognized as an SPSS command.
>Error # 1.  Command name: END
>The first word in the line is not recognized as an SPSS command.
>This command not executed.
 
It seems as if there is a limitation of 251 characters for python strings within SPSS.
The same program works if I chose a shorter string.
Are there any experiences on that? How to handle that problem?
 
Any advice is welcome.
 
Mario

Mario Giesel
Munich, Germany
Reply | Threaded
Open this post in threaded view
|

Re: Python string size problem

Ruben Geert van den Berg
Hi Mario!
 
The program below creates a data set with one single string value of length 500. In then reads it back and prints it. Although I get an error that 'end data' is not required (I always get this error with a data list command in Python) it seems to work otherwise.
 
Could you post your code as well?
 
Best,

Ruben van den Berg
Consultant Models & Methods
TNS NIPO
Email: [hidden email]
Mobiel: +31 6 24641435
Telefoon: +31 20 522 5738
Internet: www.tns-nipo.com

dataset close all.
set mpr on.
begin program.
import spss,spssdata
spss.Submit("""
data list free/v1(a500).
begin data
%s
end data."""%(500*'a'))
var=spssdata.Spssdata('v1')
values=[case[0] for case in var]
var.close()
print values
end program.
set mpr off.

 

Date: Mon, 16 Aug 2010 02:21:47 -0700
From: [hidden email]
Subject: Python string size problem
To: [hidden email]

Hello, Pythoneers,
 
I'm experimenting with a string of variables that is used as an argument of a function.
The string is a bit long, however (some 500 characters).
When I run the program I get this error message:
 
>Error # 6892.  Command name: BEGIN PROGRAM
>The string's length is more than 251.
>This command not executed.
>Error # 1.  Command name: "Coded_1113
>The first word in the line is not recognized as an SPSS command.
>Error # 1.  Command name: END
>The first word in the line is not recognized as an SPSS command.
>This command not executed.
 
It seems as if there is a limitation of 251 characters for python strings within SPSS.
The same program works if I chose a shorter string.
Are there any experiences on that? How to handle that problem?
 
Any advice is welcome.
 
Mario

Reply | Threaded
Open this post in threaded view
|

Re: Python string size problem

Jon K Peck
In reply to this post by Mario Giesel

The limit you are hitting is the same as for submitting any Statistics syntax: Lines can't be longer than 251 bytes.  In Python code, though, you can continue a line or break up the string to deal with this.  There are several ways to do this, but the simplest is this:
s = "abc\
def"

The backslash at the end of a line tells Python to append the following line.

Multiline literals can be created with triple quotes:
s="""abc
def"""

but in that case the line break is preserved in the literal.

HTH,

Jon Peck
SPSS, an IBM Company
[hidden email]
312-651-3435



From: Mario Giesel <[hidden email]>
To: [hidden email]
Date: 08/16/2010 03:24 AM
Subject: [SPSSX-L] Python string size problem
Sent by: "SPSSX(r) Discussion" <[hidden email]>





Hello, Pythoneers,
 
I'm experimenting with a string of variables that is used as an argument of a function.
The string is a bit long, however (some 500 characters).
When I run the program I get this error message:

 
>Error # 6892.  Command name: BEGIN PROGRAM
>The string's length is more than 251.
>This command not executed.
>Error # 1.  Command name: "Coded_1113
>The first word in the line is not recognized as an SPSS command.
>Error # 1.  Command name: END
>The first word in the line is not recognized as an SPSS command.
>This command not executed.

 
It seems as if there is a limitation of 251 characters for python strings within SPSS.
The same program works if I chose a shorter string.
Are there any experiences on that? How to handle that problem?

 
Any advice is welcome.
 
Mario


Reply | Threaded
Open this post in threaded view
|

AW: Python string size problem

Mario Giesel
Okay, that works.
Thanks, Jon.
Mario


Von: Jon K Peck <[hidden email]>
An: [hidden email]
Gesendet: Montag, den 16. August 2010, 14:42:20 Uhr
Betreff: Re: Python string size problem


The limit you are hitting is the same as for submitting any Statistics syntax: Lines can't be longer than 251 bytes.  In Python code, though, you can continue a line or break up the string to deal with this.  There are several ways to do this, but the simplest is this:
s = "abc\
def"

The backslash at the end of a line tells Python to append the following line.

Multiline literals can be created with triple quotes:
s="""abc
def"""

but in that case the line break is preserved in the literal.

HTH,

Jon Peck
SPSS, an IBM Company
[hidden email]
312-651-3435



From: Mario Giesel <[hidden email]>
To: [hidden email]
Date: 08/16/2010 03:24 AM
Subject: [SPSSX-L] Python string size problem
Sent by: "SPSSX(r) Discussion" <[hidden email]>





Hello, Pythoneers,
 
I'm experimenting with a string of variables that is used as an argument of a function.
The string is a bit long, however (some 500 characters).
When I run the program I get this error message:

 
>Error # 6892.  Command name: BEGIN PROGRAM
>The string's length is more than 251.
>This command not executed.
>Error # 1.  Command name: "Coded_1113
>The first word in the line is not recognized as an SPSS command.
>Error # 1.  Command name: END
>The first word in the line is not recognized as an SPSS command.
>This command not executed.

 
It seems as if there is a limitation of 251 characters for python strings within SPSS.
The same program works if I chose a shorter string.
Are there any experiences on that? How to handle that problem?

 
Any advice is welcome.
 
Mario



Mario Giesel
Munich, Germany