write Python List into spss file

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

write Python List into spss file

Eugenia Cachia
Hi all,

Newbie question:

I have a SPSS program which extensively uses python functions, one of them returns a list of tuples. I require this list of tuples to be written into SPSS file (does not exist yet) for future processing, would appreciate your help.

thanks,
Reply | Threaded
Open this post in threaded view
|

Re: write Python List into spss file

John F Hall
Eugenia

You need to post (a sample of) the SPSS syntax to the list: that way people
can more easily help.


John F Hall (Mr)
[Retired academic survey researcher]

Email:   [hidden email]
Website: www.surveyresearch.weebly.com
SPSS start page:  www.surveyresearch.weebly.com/spss-without-tears.html








-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of
Eugenia Cachia
Sent: 17 September 2013 07:24
To: [hidden email]
Subject: write Python List into spss file

Hi all,

Newbie question:

I have a SPSS program which extensively uses python functions, one of them
returns a list of tuples. I require this list of tuples to be written into
SPSS file (does not exist yet) for future processing, would appreciate your
help.

thanks,



--
View this message in context:
http://spssx-discussion.1045642.n5.nabble.com/write-Python-List-into-spss-fi
le-tp5722049.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

=====================
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: write Python List into spss file

Eugenia Cachia
as I've understand, the SPSS dataset has case data which is a list of tuples in python - and that's exactly what I have. It would be enough for me to convert my python list of tuples into spss dataset:

begin program.
import os, string, csv, re,multiprocessing, spss

def myPythonFunc():
 #do something
 return listOfTuples

myListOfTuples=myPythonFunc()
spss.StartDataStep()
datasetObj = spss.Dataset(name='myDataset')
for everyCase in myListOfTuples:
        datasetObj.cases.append(everyCase)
       
spss.EndDataStep()

end program.

Is it correct way of creating SPSS dataset please? Any help would be greatly appreciated.

thanks,
Reply | Threaded
Open this post in threaded view
|

Re: write Python List into spss file

Ruben Geert van den Berg
The following is a very simple illustration of how to create a new Dataset and pass stuff (variables, metadata, values) from Python into it. I hope the indentation is not removed by the listserv...

HTH,

Ruben

BEGIN PROGRAM.
import spss
with spss.DataStep():
   ds = spss.Dataset()
   ds.varlist.append('numvar',0)
   ds.varlist.append('strvar',1)
   ds.varlist['numvar'].label = 'Sample numeric variable'
   ds.varlist['strvar'].label = 'Sample string variable'
   ds.cases.append([1,'a'])
   ds.cases.append([2,'b'])
END PROGRAM.
Reply | Threaded
Open this post in threaded view
|

Re: write Python List into spss file

Jon K Peck
In reply to this post by Eugenia Cachia
You can use the Dataset class to create an SPSS dataset and then Submit a SAVE command to write it to disk.


Jon Peck (no "h") aka Kim
Senior Software Engineer, IBM
[hidden email]
phone: 720-342-5621




From:        Eugenia Cachia <[hidden email]>
To:        [hidden email],
Date:        09/16/2013 11:24 PM
Subject:        [SPSSX-L] write Python List into spss file
Sent by:        "SPSSX(r) Discussion" <[hidden email]>




Hi all,

Newbie question:

I have a SPSS program which extensively uses python functions, one of them
returns a list of tuples. I require this list of tuples to be written into
SPSS file (does not exist yet) for future processing, would appreciate your
help.

thanks,



--
View this message in context:
http://spssx-discussion.1045642.n5.nabble.com/write-Python-List-into-spss-file-tp5722049.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


Reply | Threaded
Open this post in threaded view
|

Re: write Python List into spss file

Jon K Peck
In reply to this post by Eugenia Cachia
Are you having a problem with this program?  It looks correct.  You might consider writingmyPythonFunc as a generator (with a yield statement) so that it can return one case at a time.


Jon Peck (no "h") aka Kim
Senior Software Engineer, IBM
[hidden email]
phone: 720-342-5621




From:        Eugenia Cachia <[hidden email]>
To:        [hidden email],
Date:        09/17/2013 02:50 AM
Subject:        Re: [SPSSX-L] write Python List into spss file
Sent by:        "SPSSX(r) Discussion" <[hidden email]>




as I've understand, the SPSS dataset has case data which is a list of tuples
in python - and that's exactly what I have. It would be enough for me to
convert my python list of tuples into spss dataset:

begin program.
import os, string, csv, re,multiprocessing, spss

def myPythonFunc():
#do something
return listOfTuples

myListOfTuples=myPythonFunc()
spss.StartDataStep()
datasetObj = spss.Dataset(name='myDataset')
for everyCase in myListOfTuples:
       datasetObj.cases.append(everyCase)

spss.EndDataStep()

end program.

Is it correct way of creating SPSS dataset please? Any help would be greatly
appreciated.

thanks,



--
View this message in context:
http://spssx-discussion.1045642.n5.nabble.com/write-Python-List-into-spss-file-tp5722049p5722051.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


Reply | Threaded
Open this post in threaded view
|

Re: write Python List into spss file

Eugenia Cachia
In reply to this post by Eugenia Cachia
I am correcting my code here, have to create the empty dataset first in order to be able to append to it later. There is a very simple code in the populateList function, has to produce a list of tuples (which it does), then the dataset has to be populated (but it remains empty):

DATASET NAME myDataset WINDOW=FRONT.

BEGIN PROGRAM.
import os,datetime, string, csv, re,multiprocessing, spss

def populateList():
 listOfTuples=[]
 for i in range(0,20):
  listOfTuples.append((i,i+1))
 return listOfTuples
 
myListOfTuples=populateList()

spss.StartDataStep()
datasetObj = spss.Dataset(name='myDataset')
for everyCase in myListOfTuples:
 datasetObj.cases.append(everyCase)
 
spss.EndDataStep()
END PROGRAM.
end program.

Could anyone point out where I've gone wrong please?

thanks,
Reply | Threaded
Open this post in threaded view
|

Re: write Python List into spss file

Jon K Peck
If this is a new dataset, you need to define the variables first as in this example code from the doc.

BEGIN PROGRAM.
import spss
spss.StartDataStep()
datasetObj = spss.Dataset(name=None)
datasetObj.varlist.append(’numvar’,0)
datasetObj.varlist.append(’strvar’,1)
datasetObj.varlist[’numvar’].label = ’Sample numeric variable’
datasetObj.varlist[’strvar’].label = ’Sample string variable’
datasetObj.cases.append([1,’a’])
datasetObj.cases.append([2,’b’])
spss.EndDataStep()
END PROGRAM.


Jon Peck (no "h") aka Kim
Senior Software Engineer, IBM
[hidden email]
phone: 720-342-5621




From:        Eugenia Cachia <[hidden email]>
To:        [hidden email],
Date:        09/17/2013 06:36 AM
Subject:        Re: [SPSSX-L] write Python List into spss file
Sent by:        "SPSSX(r) Discussion" <[hidden email]>




I am correcting my code here, have to create the empty dataset first in order
to be able to append to it later. There is a very simple code in the
populateList function, has to produce a list of tuples (which it does), then
the dataset has to be populated (but it remains empty):

DATASET NAME myDataset WINDOW=FRONT.

BEGIN PROGRAM.
import os,datetime, string, csv, re,multiprocessing, spss

def populateList():
listOfTuples=[]
for i in range(0,20):
 listOfTuples.append((i,i+1))
return listOfTuples

myListOfTuples=populateList()

spss.StartDataStep()
datasetObj = spss.Dataset(name='myDataset')
for everyCase in myListOfTuples:
datasetObj.cases.append(everyCase)

spss.EndDataStep()
END PROGRAM.
end program.

Could anyone point out where I've gone wrong please?

thanks,



--
View this message in context:
http://spssx-discussion.1045642.n5.nabble.com/write-Python-List-into-spss-file-tp5722049p5722057.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


Reply | Threaded
Open this post in threaded view
|

Re: write Python List into spss file

Albert-Jan Roskam
In reply to this post by Eugenia Cachia
from savReaderWriter import *

kwargs = dict(savFileName="c:/temp/outfile.sav", varNames=["numvar1", "numvar2", "strvar"], varTypes={"numvar1":0, "numvar2":, "strvar": 30})
records = [(0, 1, "aaaaa"), (666, 777, "bbbbb")]
with SavWriter(**kwargs) as writer:
    for record in records:
        writer.writerow(list(record))
 
Regards,
Albert-Jan


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
All right, but apart from the sanitation, the medicine, education, wine, public order, irrigation, roads, a
fresh water system, and public health, what have the Romans ever done for us?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 


From: Eugenia Cachia <[hidden email]>
To: [hidden email]
Sent: Tuesday, September 17, 2013 7:24 AM
Subject: [SPSSX-L] write Python List into spss file

Hi all,

Newbie question:

I have a SPSS program which extensively uses python functions, one of them
returns a list of tuples. I require this list of tuples to be written into
SPSS file (does not exist yet) for future processing, would appreciate your
help.

thanks,



--
View this message in context: http://spssx-discussion.1045642.n5.nabble.com/write-Python-List-into-spss-file-tp5722049.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